:root {
  --bg: #f4f5f8; --paper: #ffffff; --ink: #1f2328; --muted: #6b7280; --line: #e3e6ec; --line-strong: #cfd4dd;
  --accent: #2f6fed; --accent-ink: #ffffff; --accent-soft: #e8efff; --accent-soft-2: #d3e3fd;
  --sel: rgba(47, 111, 237, .11); --grid-line: #e1e3e6; --head-bg: #f8f9fa; --head-ink: #5f6368;
  --danger: #d23b3b; --ok: #1f9d55; --warn: #b7791f;
  --radius: 10px; --shadow: 0 8px 30px rgba(20, 30, 60, .12);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; overflow: hidden; }
a { color: var(--accent); }
b { font-weight: 600; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
code { font-family: var(--mono); font-size: 12.5px; background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; }
svg { width: 18px; height: 18px; fill: currentColor; vertical-align: middle; }

/* ---------- screens ---------- */
.screen { position: absolute; inset: 0; }
.screen.center { display: flex; align-items: center; justify-content: center; padding: 24px; overflow: auto; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.card { width: 100%; max-width: 380px; background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 28px 28px 22px; box-shadow: var(--shadow); }
.logo, .brand { font-weight: 700; letter-spacing: -.02em; display: flex; align-items: center; gap: 8px; }
.logo { font-size: 22px; margin-bottom: 6px; }
.logo::before, .brand::before { content: ""; width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='8' fill='%23000'/%3E%3Ccircle cx='11' cy='16' r='5' fill='%23fff'/%3E%3Ccircle cx='21' cy='16' r='5' fill='%23fff' fill-opacity='.75'/%3E%3C/svg%3E") center/contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect width='32' height='32' rx='8' fill='%23000'/%3E%3Ccircle cx='11' cy='16' r='5' fill='%23fff'/%3E%3Ccircle cx='21' cy='16' r='5' fill='%23fff' fill-opacity='.75'/%3E%3C/svg%3E") center/contain no-repeat; }
.brand::before { width: 20px; height: 20px; }
.card p { margin: 0 0 16px; }
label { display: block; font-size: 12.5px; font-weight: 600; color: #374151; margin: 12px 0 0; }
label input:not([type=checkbox]):not([type=color]), .ask input { display: block; width: 100%; margin-top: 5px; font: inherit; font-size: 14px; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; color: var(--ink); }
input:focus-visible, select:focus-visible, button:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; margin-top: 14px; cursor: pointer; }
.err { color: var(--danger); background: #fdecec; border: 1px solid #f5c2c2; border-radius: 8px; padding: 8px 10px; margin-top: 12px; font-size: 13px; }
.notice { background: var(--accent-soft); border: 1px solid var(--accent-soft-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; }

/* ---------- buttons ---------- */
.btn { font: inherit; font-size: 13px; font-weight: 500; color: var(--ink); background: #fff; border: 1px solid var(--line-strong); border-radius: 8px; padding: 6px 11px; cursor: pointer; line-height: 1.3; white-space: nowrap; }
.btn:hover { background: #f7f8fb; border-color: #b8bfcb; }
.btn:active { transform: translateY(.5px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #2560d6; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fdecec; border-color: #f0a8a8; }
.btn.primary.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: #eceff5; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.wide { width: 100%; margin-top: 16px; padding: 10px; font-size: 14px; }
.btn.icon { width: 32px; padding: 6px 0; text-align: center; }
.btn.on { background: var(--accent-soft); border-color: var(--accent-soft-2); color: var(--accent); }
/* toolbar icon buttons */
.tb { font: inherit; font-size: 13px; font-weight: 600; color: #444746; background: transparent; border: 0; border-radius: 6px; height: 30px; min-width: 30px; padding: 0 6px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
.tb:hover { background: #e9ecf1; }
.tb.on { background: var(--accent-soft-2); color: var(--accent); }
.tb.txt { padding: 0 10px; font-weight: 500; }
.tb.color svg { width: 20px; height: 20px; }

/* ---------- app frame ---------- */
#screen-app { display: flex; flex-direction: column; }
#screen-app:not(.ready) main { opacity: .5; pointer-events: none; }
.topbar { display: flex; align-items: center; gap: 14px; padding: 0 14px; height: 50px; background: var(--paper); border-bottom: 1px solid var(--line); flex: none; }
.brand { font-size: 16px; }
.tabs { display: flex; gap: 2px; background: #eef0f4; padding: 3px; border-radius: 9px; }
.tabs button { font: inherit; font-size: 13px; font-weight: 600; padding: 5px 16px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.tabs button:hover { color: var(--ink); }
.tabs button.active { background: #fff; color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.badge { background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; border-radius: 999px; padding: 0 6px; min-width: 18px; height: 18px; line-height: 18px; text-align: center; }
.status { font-size: 12.5px; color: var(--muted); min-width: 120px; }
.status[data-kind=saving]::before, .status[data-kind=dirty]::before { content: "●"; color: var(--warn); margin-right: 5px; font-size: 10px; }
.status[data-kind=saved]::before { content: "●"; color: var(--ok); margin-right: 5px; font-size: 10px; }
.status[data-kind=error]::before, .status[data-kind=offline]::before { content: "●"; color: var(--danger); margin-right: 5px; font-size: 10px; }
.presence { margin-left: auto; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.presence .dot { width: 8px; height: 8px; border-radius: 50%; background: #c5cad3; }
.presence .dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(31,157,85,.15); }
.presence .pname { font-weight: 600; }
#btn-menu { font-weight: 600; }
.chev { color: var(--muted); font-size: 11px; }
main { flex: 1; min-height: 0; position: relative; }
.view { position: absolute; inset: 0; display: flex; flex-direction: column; }

/* ---------- toolbars ---------- */
.toolbar { display: flex; align-items: center; gap: 4px; padding: 6px 10px; background: var(--paper); border-bottom: 1px solid var(--line); flex: none; flex-wrap: wrap; }
.toolbar .sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.toolbar .spacer { flex: 1; }
.toolbar .count, .toolbar .doc-words { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.toolbar select.fmt { font: inherit; font-size: 13px; padding: 5px 8px; border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; }
.searchwrap { display: flex; align-items: center; gap: 6px; margin: 0; padding: 0 8px; height: 30px; border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; color: var(--muted); }
.toolbar .searchwrap input.search { font: inherit; font-size: 13px; border: 0; outline: none; width: 200px; margin: 0; padding: 0; border-radius: 0; background: transparent; color: var(--ink); }
.searchwrap:focus-within { border-color: var(--accent); }

/* ---------- formula bar ---------- */
.fbar { display: flex; align-items: stretch; height: 32px; background: var(--paper); border-bottom: 1px solid var(--line); flex: none; }
.fbar .cellref { width: 78px; display: flex; align-items: center; justify-content: center; font-size: 13px; color: #444746; border-right: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.fbar .fx { width: 34px; display: flex; align-items: center; justify-content: center; font-style: italic; font-family: Georgia, serif; color: var(--muted); border-right: 1px solid var(--line); }
.fbar .fx-input { flex: 1; border: 0; outline: none; font: inherit; font-size: 13.5px; padding: 0 10px; background: transparent; color: var(--ink); }

/* ---------- grid ---------- */
.grid { flex: 1; overflow: auto; position: relative; background: var(--paper); outline: none; user-select: none; }
.ghead { position: sticky; top: 0; z-index: 5; display: flex; height: 26px; background: var(--head-bg); border-bottom: 1px solid #c8cbd0; }
.hcell { flex: none; height: 26px; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 500; color: var(--head-ink); border-right: 1px solid #d6d9de; position: relative; cursor: pointer; }
.hcell:hover { background: #eceff3; }
.hcell.on { background: var(--accent-soft-2); color: var(--accent); font-weight: 600; }
.hcell .hname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hcell.sorted-asc .hname::after { content: " ↑"; } .hcell.sorted-desc .hname::after { content: " ↓"; }
.hcell.corner { width: 50px; min-width: 50px; position: sticky; left: 0; z-index: 6; background: var(--head-bg); cursor: default; }
.hcell.addcol { width: 40px; min-width: 40px; color: var(--muted); font-size: 16px; border-right: 0; }
.resizer { position: absolute; right: -4px; top: 0; width: 8px; height: 100%; cursor: col-resize; z-index: 2; }
.resizer:hover { background: var(--accent); opacity: .6; }
.gfrozen { position: sticky; top: 26px; z-index: 4; background: var(--paper); box-shadow: 0 2px 0 #c8cbd0; }
.gbody { position: relative; }
.rows { position: absolute; left: 0; top: 0; will-change: transform; }
.row { display: flex; height: 28px; }
.cell { flex: none; height: 28px; line-height: 27px; padding: 0 6px; border-right: 1px solid var(--grid-line); border-bottom: 1px solid var(--grid-line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; position: relative; }
.cell.gutter { width: 50px; min-width: 50px; position: sticky; left: 0; z-index: 3; background: var(--head-bg); color: var(--head-ink); font-size: 11.5px; text-align: center; padding: 0; cursor: pointer; border-right: 1px solid #d6d9de; }
.cell.gutter.on, .row.rsel .cell.gutter { background: var(--accent-soft-2); color: var(--accent); font-weight: 600; }
.cell.sel { background: var(--sel); }
.cell.focus { box-shadow: inset 0 0 0 2px var(--accent); z-index: 1; background: #fff; }
.cell.focus.sel { background: var(--sel); }
.cell.fillp { background: rgba(47,111,237,.06); box-shadow: inset 0 0 0 1px var(--accent); }
.cell.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell.err { color: var(--danger); font-weight: 600; }
.cell.b { font-weight: 700; } .cell.i { font-style: italic; } .cell.u { text-decoration: underline; } .cell.st { text-decoration: line-through; } .cell.u.st { text-decoration: underline line-through; }
.cell.al-l { text-align: left; } .cell.al-c { text-align: center; } .cell.al-r { text-align: right; }
.fill-handle { position: absolute; right: 0; bottom: 0; width: 8px; height: 8px; background: var(--accent); border: 1px solid #fff; cursor: crosshair; z-index: 2; }
.empty { padding: 40px 20px; color: var(--muted); font-size: 14px; white-space: nowrap; }
.cell-editor { position: absolute; z-index: 7; font: inherit; font-size: 13px; padding: 0 5px; border: 2px solid var(--accent); border-radius: 0; background: #fff; box-shadow: 0 4px 14px rgba(0,0,0,.14); outline: none; }

/* sheet tabs */
.sheet-tabs { display: flex; align-items: center; gap: 4px; height: 36px; padding: 0 8px; background: var(--head-bg); border-top: 1px solid var(--line); flex: none; }
.sheet-tabs .spacer { flex: 1; }
.sheet-tabs .count { font-size: 12.5px; color: var(--muted); white-space: nowrap; padding-right: 6px; }
.tabs-list { display: flex; gap: 2px; overflow-x: auto; max-width: 70%; }
.stab { font: inherit; font-size: 13px; font-weight: 500; color: #444746; background: transparent; border: 0; border-bottom: 3px solid transparent; padding: 0 14px; height: 36px; cursor: pointer; white-space: nowrap; }
.stab:hover { background: #eceff3; }
.stab.active { background: #fff; color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- menus / palette ---------- */
.menu { position: fixed; z-index: 50; background: #fff; border: 1px solid var(--line-strong); border-radius: 10px; box-shadow: var(--shadow); padding: 5px; min-width: 200px; display: flex; flex-direction: column; }
.menu .mi { font: inherit; font-size: 13px; text-align: left; background: transparent; border: 0; padding: 7px 12px; border-radius: 6px; cursor: pointer; color: var(--ink); }
.menu .mi:hover { background: var(--accent-soft); }
.menu .mi.danger { color: var(--danger); }
.menu .mi.danger:hover { background: #fdecec; }
.palette { position: fixed; z-index: 50; background: #fff; border: 1px solid var(--line-strong); border-radius: 10px; box-shadow: var(--shadow); padding: 10px; width: 232px; }
.palette .sws { display: grid; grid-template-columns: repeat(8, 22px); gap: 4px; margin: 8px 0; }
.palette .sw { width: 22px; height: 22px; border: 1px solid rgba(0,0,0,.15); border-radius: 4px; cursor: pointer; padding: 0; }
.palette .sw:hover { outline: 2px solid var(--accent); }
.palette .sw.none { width: auto; height: auto; font: inherit; font-size: 12.5px; padding: 4px 10px; background: #fff; }
.palette .custom { display: flex; align-items: center; gap: 8px; font-weight: 500; font-size: 12.5px; margin: 0; }
.palette input[type=color] { width: 34px; height: 24px; border: 1px solid var(--line-strong); border-radius: 4px; padding: 0; background: #fff; }

/* ---------- doc ---------- */
.doc-scroll { flex: 1; overflow: auto; padding: 28px 16px 80px; }
.page { max-width: 820px; margin: 0 auto; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 2px 10px rgba(20,30,60,.05); min-height: 70vh; }
.doc-editor { padding: 56px 72px; outline: none; font-size: 15.5px; line-height: 1.65; min-height: 60vh; }
.doc-editor:empty::before { content: attr(data-placeholder); color: #a1a6b0; pointer-events: none; }
.doc-editor h1 { font-size: 28px; margin: .6em 0 .3em; letter-spacing: -.02em; }
.doc-editor h2 { font-size: 21px; margin: .8em 0 .3em; }
.doc-editor h3 { font-size: 17px; margin: .8em 0 .3em; }
.doc-editor p, .doc-editor div { margin: 0 0 .35em; }
.doc-editor blockquote { margin: .5em 0; padding: 4px 16px; border-left: 3px solid var(--accent-soft-2); color: #4b5563; }
.doc-editor ul, .doc-editor ol { padding-left: 26px; }
.doc-editor a { color: var(--accent); }
.doc-editor pre, .doc-editor code { font-family: var(--mono); font-size: 13px; }
.doc-banner { background: #fff7e6; color: #7a4d00; border-bottom: 1px solid #f5d89a; padding: 8px 16px; font-size: 13px; text-align: center; }
.readonly .doc-editor { background: #fbfbfc; color: #4b5563; cursor: default; }
@media (max-width: 720px) { .doc-editor { padding: 28px 22px; } .doc-scroll { padding: 12px 8px 60px; } }

/* ---------- messages ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; max-width: 900px; width: 100%; margin: 0 auto; }
.chat-log { flex: 1; overflow: auto; padding: 20px 20px 10px; display: flex; flex-direction: column; gap: 2px; }
.chat-empty { margin: auto; color: var(--muted); text-align: center; }
.chat-day { display: flex; justify-content: center; margin: 14px 0 8px; }
.chat-day span { font-size: 12px; color: var(--muted); background: #e9ecf1; border-radius: 999px; padding: 2px 10px; }
.msg { max-width: 72%; align-self: flex-start; margin-top: 8px; }
.msg.cont { margin-top: 0; }
.msg.mine { align-self: flex-end; }
.msg-meta { font-size: 12px; color: var(--muted); margin: 0 6px 3px; }
.msg-meta b { color: var(--ink); }
.msg.mine .msg-meta { text-align: right; }
.msg-body { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px; font-size: 14.5px; white-space: pre-wrap; word-break: break-word; line-height: 1.4; box-shadow: 0 1px 2px rgba(20,30,60,.04); }
.msg.mine .msg-body { background: var(--accent); border-color: var(--accent); color: #fff; }
.msg.mine .msg-body a { color: #fff; }
.msg.pending .msg-body { opacity: .7; }
.chat-form { display: flex; gap: 8px; padding: 10px 20px 16px; align-items: flex-end; }
.chat-input { flex: 1; font: inherit; font-size: 14.5px; padding: 10px 14px; border: 1px solid var(--line-strong); border-radius: 14px; resize: none; background: #fff; color: var(--ink); line-height: 1.4; max-height: 160px; }
.chat-send { height: 42px; padding: 0 18px; border-radius: 12px; }

/* ---------- dialogs ---------- */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(560px, 94vw); max-height: 90vh; box-shadow: var(--shadow); color: var(--ink); }
dialog::backdrop { background: rgba(20, 25, 40, .35); }
dialog.ask { width: min(420px, 92vw); }
dialog.ask p { margin: 6px 0 14px; white-space: pre-line; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); font-size: 15px; }
.dlg-body { padding: 8px 22px 22px; overflow: auto; max-height: calc(90vh - 52px); }
.dlg-body section { padding: 14px 0; border-bottom: 1px solid var(--line); }
.dlg-body section:last-child { border-bottom: 0; }
.dlg-body h3 { margin: 0 0 4px; font-size: 15px; }
.dlg-body h4 { margin: 14px 0 0; font-size: 13.5px; }
.dlg-body p { margin: 4px 0 10px; }
.inline-form .btn { margin-top: 12px; }
.member { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; margin: 8px 0; }
.pill { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--accent); background: var(--accent-soft); border-radius: 999px; padding: 1px 7px; margin-left: 6px; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 8px; }
.row-btns.end { justify-content: flex-end; margin-top: 14px; }
.code { font-family: var(--mono); font-size: 15px; letter-spacing: .04em; background: #f3f4f7; border: 1px dashed var(--line-strong); border-radius: 10px; padding: 14px; white-space: pre-wrap; word-break: break-all; user-select: all; margin: 10px 0; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%); background: #1b1f27; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13.5px; box-shadow: var(--shadow); z-index: 100; max-width: 90vw; }

@media (max-width: 720px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .status { display: none; }
  .presence .muted { display: none; }
  .searchwrap input { width: 110px; }
  .tb.txt span { display: none; }
  .msg { max-width: 88%; }
}
