/* Offgrid Cloud Sheets. White ground, one green accent, 6px radii. */
:root {
  --ink: #13201a;
  --muted: #55625b;
  --line: #d5dbd5;
  --surface: #f4f5f2;
  --input: #7d8a83;
  --accent: #1c5a48;
  --sel: #dcede5;
  --bg: #ffffff;
  --danger: #a3261c;
  --radius: 6px;
  --row-h: 30px;
  --names-h: 32px;
  --row-head-w: 56px;
  --font: 'Instrument Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --brand-font: 'Bricolage Grotesque', 'Instrument Sans', system-ui, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, Menlo, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 20px;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
a:hover { color: var(--ink); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
svg { flex: none; }

.boot { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.boot-line { margin: 0; color: var(--muted); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--input); border-radius: var(--radius);
  background: var(--bg); color: var(--ink);
  font-size: 13px; line-height: 16px; font-weight: 500; letter-spacing: 0.01em;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface); color: var(--ink); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #ffffff; }
.btn.primary:hover { background: #164a3b; color: #ffffff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #ffffff; }
.btn.danger:hover { background: #8a1f17; color: #ffffff; }
.btn.quiet { border-color: transparent; background: transparent; color: var(--muted); }
.btn.quiet:hover { background: var(--surface); color: var(--ink); }
.btn.wide { width: 100%; }

/* ---------- Signed out ---------- */
.signin {
  min-height: 100vh; min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px 16px; background: var(--surface);
}
.card {
  width: min(400px, 100%);
  display: flex; flex-direction: column; align-items: flex-start; gap: 12px;
  padding: 28px 24px 24px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}
.card .brand { padding: 0; font-size: 28px; line-height: 34px; }
.card-title { margin: 0; font-size: 17px; line-height: 24px; font-weight: 600; }
.card-line { margin: 0 0 8px; color: var(--muted); font-size: 15px; line-height: 22px; }
.card .btn { height: 44px; padding: 0 18px; font-size: 14px; }

/* ---------- App frame ---------- */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; background: var(--bg); }

.side {
  flex: 0 0 300px; width: 300px; min-width: 0;
  display: flex; flex-direction: column; gap: 18px;
  padding: 20px 12px 16px;
  border-right: 1px solid var(--line);
  background: var(--bg);
  overflow-y: auto;
}
.side-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.brand {
  padding-left: 4px;
  font-family: var(--brand-font); font-size: 22px; line-height: 28px; font-weight: 600; letter-spacing: -0.01em;
}
.drawer-close { display: none; }
.company { display: flex; flex-direction: column; gap: 2px; padding-left: 4px; }
.company .label { font-size: 12px; line-height: 16px; color: var(--muted); }
.company .name { font-size: 14px; line-height: 20px; font-weight: 500; overflow-wrap: anywhere; }
.side-actions { display: flex; flex-direction: column; gap: 6px; }

.tree { display: flex; flex-direction: column; gap: 1px; }
.trow { display: flex; align-items: center; gap: 2px; border-radius: var(--radius); }
.trow.top { margin-top: 6px; }
.trow.top:first-child { margin-top: 0; }
.trow.current { background: var(--sel); }
.tw, .ta {
  flex: none; display: flex; align-items: center; justify-content: center;
  padding: 0; border: 0; border-radius: var(--radius); background: transparent;
}
.tw { width: 24px; height: 30px; color: var(--ink); }
.tw-space { width: 24px; flex: none; }
.ta { width: 28px; height: 28px; color: var(--muted); opacity: 0; }
.trow:hover .ta, .trow.current .ta, .ta:focus-visible { opacity: 1; }
.trow.current .ta { color: var(--accent); }
.tw:hover, .ta:hover { background: var(--surface); }
.tn {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 8px;
  height: 30px; padding: 0 8px 0 4px;
  border: 0; border-radius: var(--radius); background: transparent;
  color: var(--ink); font-size: 14px; line-height: 20px; font-weight: 400; text-align: left;
}
.trow:not(.current) .tn:hover { background: var(--surface); }
.trow.top .tn { font-size: 15px; font-weight: 600; }
.trow.current .tn { color: var(--accent); font-weight: 600; }
.tn .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tn .ct { font-size: 12px; line-height: 16px; color: var(--muted); font-weight: 400; font-variant-numeric: tabular-nums; }
.tree-empty { margin: 0; padding: 4px; color: var(--muted); font-size: 13px; }

.side-foot {
  margin-top: auto; padding: 12px 4px 0; border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 6px;
}
.side-foot .who { font-size: 12px; line-height: 16px; color: var(--muted); overflow-wrap: anywhere; }
.side-foot .btn { align-self: flex-start; height: 32px; padding: 0 10px; }

/* ---------- Main ---------- */
.main {
  flex: 1; min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto minmax(0, 1fr);
}
.head {
  grid-column: 1; min-width: 0;
  display: flex; align-items: baseline; gap: 8px 12px;
  min-height: 56px; padding: 10px 20px 10px 20px;
  border-bottom: 1px solid var(--line);
}
.menu-btn { display: none; }
.title { min-width: 0; display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 8px; }
.crumbs { display: contents; }
.crumb {
  padding: 0; border: 0; border-radius: 4px; background: none;
  font-size: 15px; line-height: 24px; color: var(--muted); text-align: left;
}
.crumb:hover { color: var(--ink); text-decoration: underline; }
.crumb-sep { font-size: 15px; line-height: 24px; color: var(--muted); }
.title h1 { margin: 0; font-size: 15px; line-height: 24px; font-weight: 600; overflow-wrap: anywhere; }
.meta { display: flex; align-items: baseline; gap: 8px; font-size: 12px; line-height: 16px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.save { color: var(--muted); }
.save.bad { color: var(--danger); font-weight: 500; }

.findbar {
  grid-column: 2;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 20px 10px 0;
  border-bottom: 1px solid var(--line);
}
.findbar label { font-size: 13px; line-height: 16px; font-weight: 500; color: var(--muted); }
.findbar input {
  width: 240px; max-width: 100%; height: 36px; padding: 0 10px;
  border: 1px solid var(--input); border-radius: var(--radius); background: var(--bg);
  color: var(--ink); font-size: 14px;
}

.tools {
  grid-column: 1 / -1;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--line);
}
.tool {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 10px;
  border: 1px solid var(--input); border-radius: var(--radius); background: var(--bg);
  color: var(--ink); font-size: 13px; line-height: 16px; font-weight: 500; white-space: nowrap;
}
.tool:hover { background: var(--surface); }
.tool:disabled { opacity: 0.5; cursor: default; }
.tool.danger { color: var(--danger); }
.tool-sep { width: 1px; height: 20px; margin: 0 4px; background: var(--line); }

/* ---------- Grid ---------- */
.gridwrap {
  grid-column: 1 / -1; min-height: 0;
  position: relative; overflow: auto;
  background: var(--bg);
  overscroll-behavior: contain;
}
.grid-note { margin: 0; padding: 24px 20px; color: var(--muted); }
.grid-note .btn { margin-left: 8px; }
.grid {
  border-collapse: separate; border-spacing: 0; table-layout: fixed;
  font-size: 13px; line-height: 18px; color: var(--ink);
}
.grid:focus, .grid:focus-visible { outline: none; }
.grid th, .grid td { padding: 0; }
.grid thead th {
  position: sticky; z-index: 2;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.grid thead tr.letters th {
  top: 0; height: 24px;
  border-bottom: 1px solid var(--line);
  font-size: 12px; font-weight: 500; color: var(--muted); text-align: center;
}
.grid thead tr.names th {
  top: 25px; height: var(--names-h); padding: 0 10px;
  border-bottom: 2px solid var(--input);
  font-size: 13px; font-weight: 600; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: default; user-select: none;
}
.grid thead th.corner { left: 0; z-index: 3; }
.grid tbody th {
  position: sticky; left: 0; z-index: 1;
  height: calc(var(--row-h) - 1px); padding: 0 8px;
  background: var(--surface);
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: 12px; font-weight: 400; color: var(--muted); text-align: right;
  font-variant-numeric: tabular-nums;
  user-select: none;
}
.grid td {
  height: calc(var(--row-h) - 1px); padding: 0 10px;
  background: var(--bg);
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: cell;
}
.grid tbody tr:hover td { background: var(--surface); }
.grid td.sel { outline: 2px solid var(--accent); outline-offset: -2px; }
.grid td.err { color: var(--danger); }
.grid th.cur { background: var(--sel); color: var(--accent); font-weight: 600; }
.grid tr.sp td, .grid tr.sp:hover td { height: auto; padding: 0; border: 0; background: transparent; }
.col-rename {
  position: absolute; inset: 2px; width: calc(100% - 4px); height: calc(100% - 4px);
  padding: 0 8px; border: 1px solid var(--accent); border-radius: 4px;
  background: var(--bg); font-size: 13px; font-weight: 600;
}
.cell-editor {
  position: absolute; z-index: 1; margin: 0;
  padding: 0 9px;
  border: 0; border-radius: 0;
  background: var(--bg); color: var(--ink);
  font-size: 13px; line-height: 18px;
  outline: 2px solid var(--accent); outline-offset: -2px;
  box-shadow: 0 4px 14px rgba(19, 32, 26, 0.16);
}
.cell-editor:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- Dialog ---------- */
.dlg {
  width: min(440px, calc(100vw - 32px)); padding: 0;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink);
  box-shadow: 0 16px 48px rgba(19, 32, 26, 0.22);
}
.dlg::backdrop { background: rgba(19, 32, 26, 0.35); }
.dlg-form { display: flex; flex-direction: column; gap: 14px; padding: 20px; margin: 0; }
.dlg h2 { margin: 0; font-size: 17px; line-height: 24px; font-weight: 600; }
.dlg p { margin: 0; color: var(--muted); }
.dlg .field { display: flex; flex-direction: column; gap: 4px; }
.dlg label { font-size: 12px; line-height: 16px; color: var(--muted); }
.dlg input {
  height: 40px; padding: 0 10px;
  border: 1px solid var(--input); border-radius: var(--radius); background: var(--bg);
  font-size: 15px;
}
.dlg .dlg-error { color: var(--danger); font-size: 13px; }
.dlg .dlg-buttons { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Phone ---------- */
@media (max-width: 759.98px) {
  :root { --row-h: 40px; --names-h: 34px; --row-head-w: 48px; }
  .app { flex-direction: column; }

  .side {
    position: fixed; inset: 0; z-index: 50;
    width: 100%; flex: none;
    padding: 10px 12px 20px;
    border-right: 0;
    transform: translateX(-100%); visibility: hidden;
    transition: transform 0.18s ease, visibility 0s linear 0.18s;
  }
  .app.drawer-open .side { transform: none; visibility: visible; transition: transform 0.18s ease; }
  .side-top { min-height: 44px; }
  .drawer-close {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; padding: 0;
    border: 1px solid var(--input); border-radius: var(--radius); background: var(--bg);
  }
  .side-actions .btn { height: 44px; }
  .tw { width: 44px; height: 44px; }
  .tw-space { width: 44px; }
  .tn { height: 44px; font-size: 15px; }
  .trow.top .tn { font-size: 16px; }
  .ta { display: none; width: 44px; height: 44px; opacity: 1; }
  .trow.current .ta { display: flex; }
  .side-foot .btn { height: 44px; }

  .main { grid-template-columns: minmax(0, 1fr); }
  .head {
    grid-column: 1;
    align-items: center; gap: 8px;
    height: 64px; min-height: 64px; padding: 10px 12px;
  }
  .menu-btn {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; flex: none; padding: 0;
    border: 1px solid var(--input); border-radius: var(--radius); background: var(--bg);
  }
  .title { flex: 1; flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: 0; }
  .crumbs { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; line-height: 16px; }
  .crumb, .crumb-sep { font-size: 12px; line-height: 16px; }
  .crumb-sep { margin: 0 4px; }
  .title h1 { font-size: 17px; line-height: 24px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .meta { flex-direction: column; align-items: flex-end; gap: 2px; }

  .findbar { grid-column: 1; flex-direction: column; align-items: stretch; gap: 4px; padding: 10px 12px; }
  .findbar input { width: 100%; height: 44px; padding: 0 12px; font-size: 16px; }

  .tools { flex-wrap: nowrap; overflow-x: auto; padding: 8px 12px; gap: 8px; }
  .tool { height: 44px; padding: 0 12px; }
  .tool-sep { display: none; }

  .grid { font-size: 14px; line-height: 20px; }
  .grid tbody th { padding: 0 6px; }
  .cell-editor { font-size: 16px; }
  .dlg .btn { height: 44px; }
  .dlg input { height: 44px; font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .side, .app.drawer-open .side { transition: none; }
}
