:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #16181d;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #1f4fd8;
  --brand-soft: #e8eefc;
  --good: #15803d;
  --good-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #171a21; --ink: #e8eaee; --muted: #9aa1ad; --line: #272b35;
    --brand: #6d8dff; --brand-soft: #1c2440; --good-soft: #12301f; --warn-soft: #33270f; --bad-soft: #3a1717;
    --good: #4ade80; --warn: #fbbf24; --bad: #f87171;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px; line-height: 1.45;
}
a { color: var(--brand); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p.sub { color: var(--muted); margin: 0 0 20px; }
.brand-mark { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.brand-mark .logo {
  width: 36px; height: 36px; border-radius: 9px; background: var(--brand); color: #fff;
  display: grid; place-items: center; font-weight: 700;
}

/* ---------- Form controls ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
textarea { min-height: 70px; resize: vertical; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  padding: 8px 13px; border-radius: 8px; cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.block { width: 100%; justify-content: center; padding: 10px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.msg { margin-top: 12px; font-size: 13px; padding: 8px 10px; border-radius: 8px; display: none; }
.msg.show { display: block; }
.msg.err { background: var(--bad-soft); color: var(--bad); }
.msg.ok { background: var(--good-soft); color: var(--good); }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100%; }
.side {
  background: var(--panel); border-right: 1px solid var(--line); padding: 16px 12px;
  display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.side .brand-mark { padding: 0 6px; }
.side .brand-mark b { font-size: 15px; }
.side .brand-mark small { display: block; color: var(--muted); font-size: 11px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--ink); font-weight: 500;
}
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--brand-soft); color: var(--brand); }
.nav .ico { width: 18px; text-align: center; opacity: .8; }
.side .foot { margin-top: auto; border-top: 1px solid var(--line); padding-top: 12px; font-size: 12px; color: var(--muted); }
.side .foot b { color: var(--ink); display: block; font-size: 13px; }

.main { min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 12px 24px; border-bottom: 1px solid var(--line);
  background: var(--panel); position: sticky; top: 0; z-index: 5;
}
.topbar h2 { margin: 0; font-size: 17px; flex: 1; }
.topbar select { width: auto; min-width: 200px; }
.menu-btn { display: none; }
.content { padding: 24px; max-width: 1200px; }

/* ---------- Cards / tables ---------- */
.grid-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .k { color: var(--muted); font-size: 12px; font-weight: 500; }
.stat .v { font-size: 26px; font-weight: 700; margin-top: 4px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 18px; }
.card-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.card-head h3 { margin: 0; font-size: 14px; flex: 1; }
.card-body { padding: 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--bg); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td .actions { display: flex; gap: 6px; justify-content: flex-end; }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600;
  background: var(--bg); color: var(--muted); border: 1px solid var(--line); text-transform: capitalize;
}
.badge.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.brand { background: var(--brand-soft); color: var(--brand); border-color: transparent; }
.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs button { border: 1px solid var(--line); background: var(--panel); padding: 7px 12px; border-radius: 8px; cursor: pointer; }
.tabs button.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.notice { background: var(--warn-soft); color: var(--warn); padding: 12px 14px; border-radius: 8px; margin-bottom: 16px; }

/* ---------- Modal ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(10,12,16,.45); display: none; align-items: flex-start;
  justify-content: center; padding: 40px 16px; z-index: 50; overflow-y: auto;
}
.overlay.show { display: flex; }
.modal { background: var(--panel); border-radius: 12px; width: 100%; max-width: 520px; box-shadow: var(--shadow); }
.modal-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 15px; flex: 1; }
.modal-body { padding: 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.toast {
  position: fixed; bottom: 20px; right: 20px; background: var(--ink); color: var(--panel); padding: 10px 14px;
  border-radius: 8px; opacity: 0; transform: translateY(8px); transition: .2s; z-index: 60; max-width: 340px;
}
.toast.show { opacity: 1; transform: none; }
.toast.err { background: var(--bad); color: #fff; }

/* ---------- Work Hub parts ---------- */
.stat .v.bad { color: var(--bad); }
.barwrap { display: flex; align-items: center; gap: 8px; min-width: 110px; }
.bar { flex: 1; height: 6px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 99px; }
.late { color: var(--bad); font-weight: 600; }
.warn-text { color: var(--warn); font-weight: 600; }
tr.dim td { opacity: .6; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px 20px; }
.kv > div > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; font-weight: 500; }
.pre { white-space: pre-wrap; word-break: break-word; }
td.pre { max-width: 340px; }
.thread .c { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
.thread .c.internal { background: var(--warn-soft); border-color: transparent; }
.thread .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
select.inline { width: auto; padding: 5px 8px; font-size: 12px; }
.manage { align-items: flex-start; }
.manage .field { flex: 1 1 160px; margin-bottom: 12px; }
.card-body .notice:last-child { margin-bottom: 0; }

/* ---------- Tags ---------- */
.tag { display: inline-block; padding: 1px 7px; margin: 2px 3px 0 0; border-radius: 6px; font-size: 11px;
  background: var(--brand-soft); color: var(--brand); font-weight: 500; }
.view-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.view-toggle button { border: 0; background: var(--panel); padding: 6px 11px; cursor: pointer; font-size: 12px; }
.view-toggle button.on { background: var(--brand); color: #fff; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .tabs { margin-bottom: 0; }
.toolbar .grow { flex: 1; }

/* ---------- Kanban ---------- */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(250px, 1fr); gap: 12px;
  overflow-x: auto; padding-bottom: 8px; margin-bottom: 18px; align-items: start; }
.col { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); min-height: 140px; display: flex; flex-direction: column; }
.col.over { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.col-head { padding: 10px 12px; font-weight: 600; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.col-head .n { margin-left: auto; color: var(--muted); font-weight: 500; }
.col-body { padding: 0 8px 8px; display: flex; flex-direction: column; gap: 8px; }
.col .quick { margin: 0 8px 8px; width: calc(100% - 16px); font-size: 13px; }
.kcard { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px; cursor: pointer; box-shadow: 0 1px 1px rgba(0,0,0,.03); }
.kcard:hover { border-color: var(--muted); }
.kcard[draggable=true] { cursor: grab; }
.kcard.dragging { opacity: .45; }
.kcard .t { font-weight: 600; margin: 4px 0; word-break: break-word; }
.kcard .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--muted); margin-top: 6px; }
.kcard .top { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--muted); }

/* ---------- Notifications ---------- */
.bell-wrap { position: relative; }
.bell { position: relative; }
.bell-count { position: absolute; top: -6px; right: -6px; background: var(--bad); color: #fff; border-radius: 999px;
  font-size: 10px; min-width: 17px; height: 17px; display: none; align-items: center; justify-content: center; padding: 0 4px; font-weight: 700; }
.bell-count.show { display: inline-flex; }
.bell-panel { position: absolute; right: 0; top: 36px; width: 340px; max-width: calc(100vw - 32px); max-height: 420px; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); display: none; z-index: 30; }
.bell-panel.show { display: block; }
.bell-panel .n-item { display: block; padding: 10px 14px; border-bottom: 1px solid var(--line); color: var(--ink); cursor: pointer; }
.bell-panel .n-item:hover { background: var(--bg); }
.bell-panel .n-item.unread { background: var(--brand-soft); }
.bell-panel .n-item small { color: var(--muted); display: block; }
.bell-panel .n-head { display: flex; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600; }
.bell-panel .n-head button { margin-left: auto; }

/* ---------- Checklist / misc ---------- */
.check-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
.check-row input[type=checkbox] { width: auto; }
.check-row.done span { text-decoration: line-through; color: var(--muted); }
.inline-add { display: flex; gap: 8px; margin-top: 6px; }
.money { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
th.money { text-align: right; }

/* ---------- Document preview (letters / policies inside the Hub) ---------- */
.doc-preview p { margin: 0 0 10px; }
.doc-preview h3 { font-size: 14px; margin: 14px 0 6px; }
.doc-preview ul { margin: 0 0 10px; padding-left: 20px; }
.doc-preview .doc-table { width: 100%; border-collapse: collapse; margin: 6px 0 12px; }
.doc-preview .doc-table th, .doc-preview .doc-table td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; }
.doc-preview .doc-table .num { text-align: right; }
.doc-preview .doc-head { display: flex; gap: 14px; border-bottom: 2px solid currentColor; padding-bottom: 10px; }
.doc-preview .doc-head img { max-height: 48px; max-width: 140px; }
.doc-preview .doc-head h1 { font-size: 17px; margin: 0; }
.doc-preview .doc-head .division { font-weight: 600; color: #1f4fd8; }
.doc-preview .doc-head .addr, .doc-preview .doc-head .ids { font-size: 12px; color: #5b6270; white-space: pre-wrap; }
.doc-preview .doc-footer { margin-top: 14px; padding-top: 6px; border-top: 1px solid #d9dde3; color: #5b6270; font-size: 11px; text-align: center; }
code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; left: 0; top: 0; width: 250px; z-index: 40; transform: translateX(-100%); transition: .2s; box-shadow: var(--shadow); }
  .side.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .topbar select { min-width: 0; flex: 1 1 100%; order: 3; }
  .board { grid-auto-columns: minmax(230px, 85vw); }
  .content { padding: 16px; }
  .two { grid-template-columns: 1fr; }
}

/* ---------- Phase 5: chat, company tree, files, vault ---------- */
.nav-badge { display: none; margin-left: auto; min-width: 18px; padding: 0 6px; border-radius: 9px; background: var(--bad); color: #fff; font-size: 11px; line-height: 18px; text-align: center; font-weight: 600; }
.nav-badge.show { display: inline-block; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; }

.chat-wrap { display: grid; grid-template-columns: 260px 1fr; height: calc(100vh - 170px); min-height: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 8px; }
.chat-list { border-right: 1px solid var(--line); overflow-y: auto; }
.chat-actions { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.chat-item { display: block; padding: 10px 12px; border-bottom: 1px solid var(--line); color: var(--ink); }
.chat-item:hover { background: var(--bg); }
.chat-item.on { background: var(--brand-soft); }
.chat-item .t { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.chat-item .muted { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-main { display: flex; flex-direction: column; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.chat-head .muted { font-size: 12px; flex: 1; }
.chat-msgs { flex: 1; overflow-y: auto; padding: 14px; background: var(--bg); }
.chat-input { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); align-items: flex-end; }
.chat-input textarea { resize: none; }
.msg-row { display: flex; margin: 4px 0; }
.msg-row.mine { justify-content: flex-end; }
.bubble { max-width: 72%; padding: 7px 11px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); }
.msg-row.mine .bubble { background: var(--brand-soft); border-color: transparent; }
.bubble .who { font-size: 12px; font-weight: 600; color: var(--brand); margin-bottom: 2px; }
.bubble .txt { word-wrap: break-word; overflow-wrap: anywhere; }
.bubble .when { font-size: 11px; color: var(--muted); margin-top: 3px; }
.bubble.deleted .txt { color: var(--muted); }
.chat-msgs .day { text-align: center; font-size: 11px; color: var(--muted); margin: 12px 0 6px; }
@media (max-width: 760px) {
  .chat-wrap { grid-template-columns: 1fr; height: auto; }
  .chat-list { max-height: 200px; border-right: 0; border-bottom: 1px solid var(--line); }
  .chat-msgs { height: 55vh; }
  .bubble { max-width: 88%; }
}

.org-scroll { overflow-x: auto; }
.org-tree, .org-tree ul { list-style: none; margin: 0; padding: 0; }
.org-tree ul { padding-left: 28px; border-left: 1px dashed var(--line); margin-left: 14px; }
.org-tree li { position: relative; padding: 6px 0; }
.org-tree ul > li::before { content: ''; position: absolute; left: -28px; top: 30px; width: 24px; border-top: 1px dashed var(--line); }
.org-tree.flat { display: flex; flex-wrap: wrap; gap: 10px; }
.org-tree.flat li { padding: 0; }
.org-card { display: inline-block; min-width: 210px; max-width: 280px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.org-card.me { border-color: var(--brand); }
.org-name { font-weight: 600; }
.org-role { font-size: 12.5px; }
.org-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; font-size: 12px; min-height: 4px; }

.grid-folders { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.folder-card { display: flex; gap: 12px; padding: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); color: var(--ink); box-shadow: var(--shadow); }
.folder-card:hover { border-color: var(--brand); }
.folder-card .ico { font-size: 26px; line-height: 1; }

/* ---------- Phase 6: goals, invoices, finance chart ---------- */
.goal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.goal-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); margin-bottom: 12px; }
.goal-grid .goal-card { margin-bottom: 0; }
.inv-item { display: grid; grid-template-columns: 1fr 90px 70px 110px auto; gap: 6px; margin-bottom: 6px; align-items: center; }
@media (max-width: 640px) { .inv-item { grid-template-columns: 1fr 1fr; } .inv-item .ii-d { grid-column: 1 / -1; } }
.fin-chart { display: flex; align-items: flex-end; gap: 8px; height: 220px; overflow-x: auto; }
.fin-col { flex: 1; min-width: 34px; display: flex; flex-direction: column; align-items: center; height: 100%; font-size: 11px; }
.fin-bars { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; gap: 3px; }
.fin-bars div { width: 40%; min-height: 1px; border-radius: 3px 3px 0 0; }
.fin-bars .in, .fin-key.in { background: var(--good); }
.fin-bars .out, .fin-key.out { background: var(--bad); opacity: .75; }
.fin-key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin: 0 4px 0 10px; vertical-align: middle; }

/* ---------- Phase 7: links ---------- */
.link-row { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.link-row:last-of-type { border-bottom: 0; }
.link-main { min-width: 0; flex: 1 1 320px; }
.link-url { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.overlay .modal { max-height: 92vh; overflow-y: auto; }
