:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #1d2330;
  --text-2: #5b6474;
  --text-3: #8a93a3;
  --primary: #3b5bdb;
  --primary-hover: #304bc0;
  --primary-soft: #eef2ff;
  --success: #12875a;
  --success-hover: #0e7049;
  --danger: #c92a2a;
  --danger-soft: #fff0f0;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 20px 45px -12px rgba(16, 24, 40, .28);
  --topbar-h: 60px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
[hidden] { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-wrap; word-break: break-word; }

/* ---------- buttons & inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 16px;
  font: inherit; font-weight: 600;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:focus-visible, .tool-btn:focus-visible, .chip:focus-visible { outline: 3px solid rgba(59, 91, 219, .35); outline-offset: 1px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: var(--success-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--surface); color: var(--danger); border-color: #f1c4c4; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-small { height: 32px; padding: 0 12px; font-size: 13px; }
.btn-block { width: 100%; }

.link-btn { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.link-btn:hover { text-decoration: underline; }

.icon-btn {
  width: 32px; height: 32px; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-2); font-size: 15px; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.tool-btn {
  height: 28px; padding: 0 10px; font: inherit; font-size: 12px; font-weight: 500;
  color: var(--text-2); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; cursor: pointer;
}
.tool-btn:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); }
.tool-btn:disabled { opacity: .45; cursor: default; }

label { display: block; font-weight: 500; margin-bottom: 14px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 6px;
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 9px 12px;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59, 91, 219, .15); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }

/* ---------- flashes & toast ---------- */

.flashes { position: fixed; top: calc(var(--topbar-h) + 12px); left: 50%; transform: translateX(-50%); z-index: 60; display: grid; gap: 8px; width: min(560px, calc(100% - 32px)); }
.flash { padding: 11px 14px; border-radius: var(--radius-sm); background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-lg); font-weight: 500; }
.flash-success { border-left: 4px solid var(--success); }
.flash-error { border-left: 4px solid var(--danger); }
body.auth .flashes { top: 24px; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 80;
  max-width: min(520px, calc(100% - 32px));
  padding: 11px 16px; border-radius: var(--radius-sm);
  background: #1d2330; color: #fff; font-weight: 500; box-shadow: var(--shadow-lg);
}
.toast-error { background: var(--danger); }

/* ---------- top bar ---------- */

.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 20px;
  padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: relative; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--text); font-size: 15px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, #3b5bdb, #12875a); color: #fff; font-size: 12px; font-weight: 700; letter-spacing: .02em;
}
.topbar-center { flex: 1; display: flex; justify-content: center; min-width: 0; }
.task-form { display: flex; gap: 8px; width: min(640px, 100%); }
.task-form input { margin: 0; height: 38px; background: var(--surface-2); }
.user-nav { display: flex; align-items: center; gap: 18px; font-weight: 500; }
.user-nav a { color: var(--text-2); }
.user-nav a:hover { color: var(--text); text-decoration: none; }
.user-nav form { margin: 0; }
.user-nav .link-btn { color: var(--text-2); font-weight: 500; }

/* ---------- dashboard ---------- */

body.app { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
body.resizing { cursor: col-resize; user-select: none; }

.workspace { flex: 1; min-height: 0; display: flex; padding: 16px; gap: 0; }
.panel {
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-left { flex: 0 0 50%; }
.panel-right { flex: 1 1 0; }
.gutter { flex: 0 0 16px; cursor: col-resize; position: relative; }
.gutter::after {
  content: ""; position: absolute; top: 50%; left: 50%; width: 4px; height: 40px; transform: translate(-50%, -50%);
  border-radius: 4px; background: var(--border-strong); transition: background .15s, height .15s;
}
.gutter:hover::after, body.resizing .gutter::after { background: var(--primary); height: 64px; }

.panel-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 48px; padding: 8px 16px; border-bottom: 1px solid var(--border); background: var(--surface-2);
}
.panel-label { font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-2); }
.panel-link { font-size: 13px; font-weight: 500; }
.panel-tools { display: flex; align-items: center; gap: 6px; }
.save-state { font-size: 12px; color: var(--text-3); margin-right: 6px; }
.panel-body { flex: 1; min-height: 0; overflow-y: auto; padding: 20px 24px; }
.panel-footer {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface-2);
}
.counts { font-size: 12px; color: var(--text-3); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.editor-wrap { padding: 0; display: flex; }
#draft {
  flex: 1; margin: 0; border: 0; border-radius: 0; resize: none;
  padding: 20px 24px; font-size: 14.5px; line-height: 1.7; background: var(--surface);
}
#draft:focus { box-shadow: none; }
#draft.flash-update { animation: flash-update 1.2s ease-out; }
@keyframes flash-update { from { background: #eafaf2; } to { background: var(--surface); } }

/* empty / loading states */
.empty-state { height: 100%; display: grid; place-content: center; text-align: center; max-width: 360px; margin: 0 auto; color: var(--text-2); }
.empty-state h2 { color: var(--text); margin: 10px 0 6px; }
.empty-state p { margin: 0; }
.empty-icon {
  width: 52px; height: 52px; margin: 0 auto; display: grid; place-items: center;
  border-radius: 14px; background: var(--primary-soft); color: var(--primary); font-size: 22px; font-weight: 700;
}
.empty-state.error .empty-icon { background: var(--danger-soft); color: var(--danger); }

.skeleton { display: grid; gap: 12px; }
.skeleton span { height: 14px; border-radius: 6px; background: linear-gradient(90deg, #eef0f3 25%, #e2e5ea 50%, #eef0f3 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.skeleton span:first-child { height: 26px; width: 70%; }
.skeleton span:nth-child(3) { width: 85%; }
.skeleton span:last-child { width: 55%; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* task content */
.task-head { padding-bottom: 18px; margin-bottom: 18px; border-bottom: 1px solid var(--border); }
.task-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.status {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text); background: color-mix(in srgb, var(--status) 18%, white);
}
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--status); }
.task-title { font-size: 20px; line-height: 1.3; margin: 0 0 14px; letter-spacing: -.01em; }
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 20px; margin: 0; }
.fact dt { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); }
.fact dd { margin: 2px 0 0; font-weight: 500; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 14px; }
.tag { padding: 2px 9px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12px; color: var(--text-2); }

.task-section { margin-bottom: 24px; }
.task-section h3 { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin: 0 0 10px; }

.prose { font-size: 14.5px; line-height: 1.7; word-break: break-word; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose h1, .prose h2, .prose h3 { font-size: 15px; margin: 18px 0 6px; }
.prose ul, .prose ol { padding-left: 22px; }
.prose code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.prose pre { background: var(--surface-2); padding: 12px; border-radius: var(--radius-sm); overflow-x: auto; }
.prose blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.prose img { max-width: 100%; border-radius: var(--radius-sm); }
.prose table { border-collapse: collapse; }
.prose td, .prose th { border: 1px solid var(--border); padding: 6px 10px; }

.fields { width: 100%; border-collapse: collapse; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.fields th, .fields td { text-align: left; vertical-align: top; padding: 9px 12px; border-bottom: 1px solid var(--border); }
.fields tr:last-child th, .fields tr:last-child td { border-bottom: 0; }
.fields th { width: 38%; font-weight: 500; color: var(--text-2); background: var(--surface-2); }

.attachments { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.comment { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 8px; background: var(--surface-2); }
.comment-meta { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--text-3); margin-bottom: 4px; }
.comment-meta strong { color: var(--text); }

/* ---------- chat ---------- */

.chat {
  position: fixed; right: 24px; bottom: 24px; z-index: 40;
  width: 420px; height: min(640px, calc(100vh - var(--topbar-h) - 48px));
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow-lg);
  overflow: hidden; animation: pop-in .18s ease-out;
}
@keyframes pop-in { from { opacity: 0; transform: translateY(12px) scale(.98); } }
.chat-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.chat-log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; background: var(--surface-2); }
.bubble { max-width: 88%; padding: 10px 13px; border-radius: 14px; white-space: pre-wrap; word-break: break-word; }
.bubble-user { align-self: flex-end; background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.bubble-assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble-error { align-self: stretch; background: var(--danger-soft); color: var(--danger); border: 1px solid #f1c4c4; font-size: 13px; }
.bubble-note { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--border); font-size: 12px; font-weight: 600; color: var(--success); }
.typing { display: flex; gap: 4px; padding: 14px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: bounce 1s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px 0; }
.chip {
  flex: none; padding: 5px 11px; font: inherit; font-size: 12px; font-weight: 500;
  color: var(--primary); background: var(--primary-soft); border: 1px solid #dbe3ff; border-radius: 999px; cursor: pointer;
}
.chip:hover { background: #e2e8ff; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px 12px; }
.chat-form textarea { margin: 0; resize: none; max-height: 140px; }

/* ---------- modal ---------- */

.modal-backdrop { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; background: rgba(16, 24, 40, .45); }
.modal {
  width: min(720px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow-lg); animation: pop-in .18s ease-out;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-body textarea { resize: vertical; line-height: 1.6; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 16px 16px; }
.loading { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 48px 0; color: var(--text-2); font-weight: 500; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border-strong); border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- auth & admin pages ---------- */

.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 600px at 20% -10%, #e4e9ff, transparent), radial-gradient(900px 500px at 110% 110%, #ddf5ea, transparent), var(--bg); }
.auth-card { width: min(400px, 100%); padding: 32px; }
.auth-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; margin-bottom: 24px; }
.auth-card h1 { font-size: 22px; }
.auth-card > p.muted { margin: 0 0 22px; }
.auth-card .small { text-align: center; margin: 16px 0 0; }

.page-wrap { max-width: 1120px; margin: 0 auto; padding: 32px 20px 64px; }
.page-wrap.narrow { max-width: 520px; }
.page-wrap > p.muted { margin: 0 0 24px; }
.form-card { padding: 24px; }
.table-card { padding: 24px; margin-top: 20px; }
.admin-grid { display: grid; grid-template-columns: 340px 1fr; gap: 20px; align-items: start; }
.admin-grid .table-card { margin-top: 0; }
.table-scroll { overflow-x: auto; }
.users-table { width: 100%; border-collapse: collapse; }
.users-table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); font-weight: 600; padding: 0 10px 10px; border-bottom: 1px solid var(--border); }
.users-table td { padding: 12px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.users-table tr:last-child td { border-bottom: 0; }
.inline-form { display: flex; gap: 6px; }
.inline-form input { margin: 0; height: 32px; padding: 4px 10px; min-width: 140px; }
.role { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); border: 1px solid var(--border); }
.role-superadmin { background: var(--primary-soft); color: var(--primary); border-color: #dbe3ff; }

/* ---------- responsive ---------- */

@media (max-width: 960px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 10px 16px; gap: 10px; }
  .topbar-center { order: 3; flex-basis: 100%; }
  .brand-name { display: none; }
  .user-nav { margin-left: auto; }
  body.app { height: auto; overflow: auto; }
  .workspace { flex-direction: column; padding: 12px; gap: 12px; }
  .panel-left { flex: none !important; max-height: 70vh; }
  .panel-right { flex: none; height: 75vh; }
  .gutter { display: none; }
  .admin-grid { grid-template-columns: 1fr; }
  .chat { left: 12px; right: 12px; bottom: 12px; width: auto; height: 75vh; }
}
