:root {
  --ink: #1c2622;
  --muted: #5f6b64;
  --paper: #f3efe6;
  --panel: #fffdf8;
  --line: #e0d8c8;
  --rail: #1a2824;
  --rail-soft: #243530;
  --rail-text: #e7efe9;
  --rail-dim: #a8b8ae;
  --copper: #c4622d;
  --copper-dark: #8d431c;
  --calc: #e5f0f2;
  --total: #f4ead0;
  --danger: #9c2f2f;
  --shadow: 0 18px 50px rgba(28, 38, 34, 0.12);
  font-family: "Segoe UI", "Helvetica Neue", sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.login {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(280px, 1.1fr) minmax(280px, 0.9fr);
  min-height: 0;
}
.login-panel {
  background:
    radial-gradient(900px 420px at -10% -20%, rgba(196, 98, 45, 0.35), transparent 55%),
    var(--rail);
  color: var(--rail-text);
  padding: 64px 56px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.kicker {
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 12px;
  color: #f0c2a4;
  margin: 0 0 12px;
}
.login-panel h1 {
  font-family: Palatino, "Palatino Linotype", Georgia, serif;
  font-size: 56px;
  font-weight: 600;
  margin: 0 0 16px;
  letter-spacing: -0.03em;
}
.lede { max-width: 460px; line-height: 1.5; color: #d5e2da; margin: 0 0 28px; }
.login-points { margin: 0; padding: 0; list-style: none; }
.login-points li {
  padding: 8px 0 8px 18px;
  border-top: 1px solid rgba(255,255,255,0.08);
  position: relative;
}
.login-points li::before {
  content: "";
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--copper);
  position: absolute;
  left: 0; top: 14px;
}
.login-card {
  margin: auto;
  width: min(420px, calc(100% - 48px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 32px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.login-card h2 {
  font-family: Palatino, "Palatino Linotype", Georgia, serif;
  margin: 0 0 8px;
  font-size: 28px;
}
label { font-size: 13px; color: var(--muted); }
input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 10px 12px;
}
input:focus, textarea:focus { outline: 2px solid rgba(196, 98, 45, 0.45); border-color: var(--copper); }
.hint { color: var(--muted); font-size: 13px; line-height: 1.45; }
.form-error { color: var(--danger); margin: 4px 0; font-size: 14px; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 8px 14px;
  white-space: nowrap;
}
.btn.primary { background: var(--copper); border-color: var(--copper); color: #fff; }
.btn.primary:hover { background: var(--copper-dark); }
.btn.ghost { background: transparent; }
.login-card .btn.primary { margin-top: 8px; padding: 12px 16px; }

.shell { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 300px;
  flex: none;
  background: var(--rail);
  color: var(--rail-text);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.side-brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 18px 16px 8px;
}
.side-brand strong { display: block; font-family: Palatino, Georgia, serif; font-size: 20px; }
.side-brand span, #side-stats { color: var(--rail-dim); font-size: 12px; }
.mark {
  width: 36px; height: 36px; border-radius: 10px;
  background:
    linear-gradient(var(--copper), var(--copper)) 6px 16px / 24px 3px no-repeat,
    linear-gradient(#efe6d4, #efe6d4) 6px 10px / 18px 3px no-repeat,
    linear-gradient(#efe6d4, #efe6d4) 6px 22px / 14px 3px no-repeat,
    #314840;
}
.search-label { padding: 10px 16px 4px; font-size: 12px; color: var(--rail-dim); }
#nav-search {
  margin: 0 16px 12px;
  width: calc(100% - 32px);
  background: var(--rail-soft);
  color: var(--rail-text);
  border-color: transparent;
}
#nav { overflow: auto; padding: 0 10px 16px; }
.cat-label, .group-label {
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #f0c2a4;
  margin: 14px 8px 6px;
}
.group-label { color: var(--rail-dim); letter-spacing: 0.08em; margin-top: 8px; }
.book-btn {
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--rail-text);
  border: 0;
  border-radius: 10px;
  padding: 8px 10px;
}
.book-btn small { display: block; color: var(--rail-dim); font-size: 11px; margin-top: 2px; }
.book-btn:hover, .book-btn.active { background: var(--rail-soft); }
.book-btn.active { box-shadow: inset 3px 0 0 var(--copper); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: rgba(255, 253, 248, 0.92);
  border-bottom: 1px solid var(--line);
}
.crumb { margin: 0; flex: 1; color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.save-state { color: #2f6f4e; font-size: 12px; }
#menu-btn { display: none; }

.workspace { flex: 1; min-height: 0; overflow: auto; padding: 20px; }
.home-head h2, .book-head h2 {
  font-family: Palatino, "Palatino Linotype", Georgia, serif;
  font-size: 34px;
  margin: 0 0 8px;
}
.home-head p, .book-note { margin: 0 0 16px; color: var(--muted); max-width: 760px; line-height: 1.5; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 14px; }
.card {
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 8px 24px rgba(28, 38, 34, 0.04);
}
.card strong { display: block; font-size: 18px; margin-bottom: 6px; }
.card span { color: var(--muted); font-size: 13px; line-height: 1.4; display: block; }
.card em { font-style: normal; color: var(--copper-dark); font-size: 12px; letter-spacing: 0.04em; }
.model-list { display: grid; gap: 8px; margin-top: 8px; }
.model-list button { text-align: left; }

.book-view { height: 100%; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.book-head { display: flex; justify-content: space-between; gap: 12px 20px; align-items: flex-start; flex-wrap: wrap; }
.book-head > div:first-child { flex: 1 1 280px; min-width: 0; }
.file-path { color: var(--muted); font-size: 13px; margin: 0; }
.toolbar, .formula-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tabs { display: flex; gap: 6px; overflow: auto; padding-bottom: 2px; }
.tab {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 6px 12px;
  color: var(--muted);
}
.tab.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.formula-row { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px; }
#cell-ref {
  min-width: 64px;
  text-align: center;
  font-weight: 700;
  color: var(--copper-dark);
}
#formula-input { flex: 1; min-width: 180px; border-radius: 8px; resize: vertical; min-height: 38px; }
#cell-result { color: var(--muted); font-size: 13px; min-width: 80px; }
.links { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.links span { color: var(--muted); font-size: 12px; }
.chip {
  border: 1px solid #c9d7dc;
  background: var(--calc);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
}
.legend { color: var(--muted); font-size: 12px; margin: 0; }
.legend i { font-style: normal; padding: 1px 6px; border-radius: 6px; margin-right: 4px; }
.swatch-input { background: #fffdf9; box-shadow: inset 2px 0 0 #d7a07a; }
.swatch-formula { background: var(--calc); }
.swatch-total { background: var(--total); }

.grid-wrap {
  flex: 1;
  min-height: 240px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
}
table.sheet { border-collapse: separate; border-spacing: 0; font-size: 12.5px; }
table.sheet th, table.sheet td {
  border-right: 1px solid #e6dfd2;
  border-bottom: 1px solid #e6dfd2;
  padding: 4px 8px;
  white-space: nowrap;
  max-width: 420px;
  vertical-align: middle;
}
table.sheet thead th {
  position: sticky; top: 0; z-index: 3;
  background: #ebe4d6;
  font-weight: 650;
  text-align: center;
  color: #5c564c;
}
table.sheet .rh, table.sheet .corner {
  position: sticky; left: 0; z-index: 2;
  background: #ebe4d6;
  min-width: 46px;
  text-align: center;
  color: #6d6456;
  font-weight: 650;
}
table.sheet thead .corner { z-index: 4; }
td.kind-input { background: #fffdf9; box-shadow: inset 2px 0 0 #d7a07a; }
td.kind-formula { background: var(--calc); color: #16343c; }
td.kind-total { background: var(--total); font-weight: 700; }
td.kind-empty { background: #fcfbf8; }
td.kind-error { background: #fdecec; color: var(--danger); font-weight: 700; }
td.kind-text { white-space: normal; min-width: 200px; max-width: 360px; }
td.merged { white-space: normal; min-width: 220px; font-weight: 650; background: #f7f1e6; vertical-align: middle; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.is-selected { box-shadow: inset 0 0 0 2px var(--copper) !important; }
tr.is-active .rh { background: #f3d7c4; color: var(--ink); }

.find-meta { color: var(--muted); font-size: 12px; }
.loading { padding: 24px; color: var(--muted); }

.site-footer {
  flex: none;
  text-align: center;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: #f7f4ee;
  font-size: 13px;
  color: var(--muted);
}
.site-footer a { color: var(--copper-dark); font-weight: 650; }

dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 22px;
  width: min(420px, calc(100% - 32px));
  background: var(--panel);
}
dialog h3 { font-family: Palatino, Georgia, serif; margin: 0 0 12px; }
dialog form { display: flex; flex-direction: column; gap: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
dialog::backdrop { background: rgba(26, 40, 36, 0.45); }

#toast {
  position: fixed;
  bottom: 56px;
  right: 16px;
  background: var(--ink);
  color: #fff;
  padding: 10px 14px;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  max-width: 320px;
}
#toast.show { opacity: 1; }

@media (max-width: 760px) {
  .login { grid-template-columns: 1fr; }
  .login-panel { padding: 32px 24px; }
  .login-panel h1 { font-size: 40px; }
  #menu-btn { display: inline-flex; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    height: 100%;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .top-actions #who { display: none; }
}
