:root {
  --navy: #263056;
  --sea: #195190;
  --viridian: #009499;
  --coral: #FF6F61;
  --yellow: #FFDA29;
  --limpet: #98DDDE;
  --turtle: #81894E;
  --pink: #DE98AB;

  --bg: #F4F6F9;
  --surface: #FFFFFF;
  --surface-2: #EEF2F7;
  --ink: #1B2140;
  --ink-2: #4B5578;
  --ink-3: #7C86A6;
  --line: #D9DFEA;
  --line-strong: #B9C3D6;
  --focus: #195190;
  --accent: var(--viridian);
  --danger: #C7463B;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(38, 48, 86, .06), 0 4px 16px rgba(38, 48, 86, .06);
  --font: 'Barlow', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-cond: 'Barlow Condensed', 'Barlow', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--sea); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--navy); color: #fff;
}
.brand { display: flex; align-items: center; gap: 12px; color: #fff; text-decoration: none; }
.brand-mark { display: grid; grid-template-columns: 9px 9px; gap: 3px; }
.brand-mark i { width: 9px; height: 9px; border-radius: 2px; display: block; }
.brand-mark i:nth-child(1) { background: var(--limpet); }
.brand-mark i:nth-child(2) { background: var(--viridian); }
.brand-mark i:nth-child(3) { background: var(--yellow); }
.brand-mark i:nth-child(4) { background: var(--coral); }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-family: var(--font-cond); font-size: 19px; letter-spacing: .2px; font-weight: 600; }
.brand-text small { font-size: 11.5px; color: var(--limpet); letter-spacing: .6px; text-transform: uppercase; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px; border-radius: 8px;
  border: 1px solid var(--line-strong); background: var(--surface);
  cursor: pointer; font-weight: 500; font-size: 14px; white-space: nowrap;
  text-decoration: none; color: var(--ink);
}
.btn:hover { border-color: var(--sea); }
.btn.primary { background: var(--viridian); border-color: var(--viridian); color: #fff; }
.btn.primary:hover { background: #007f84; }
.btn.ghost { background: transparent; border-color: rgba(255,255,255,.35); color: #fff; }
.btn.ghost:hover { border-color: #fff; }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.link { border: none; background: none; color: var(--sea); padding: 4px 6px; }
.btn.danger { color: var(--danger); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.icon-btn {
  width: 28px; height: 28px; border-radius: 6px; border: 1px solid transparent;
  background: none; cursor: pointer; color: var(--ink-3); display: inline-grid; place-items: center;
  font-size: 15px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.del:hover { color: var(--danger); }

/* Home */
.home { max-width: 1040px; margin: 0 auto; padding: 40px 20px 60px; }
.hero h1 { font-family: var(--font-cond); font-weight: 600; font-size: 40px; line-height: 1.05; margin: 0 0 10px; color: var(--navy); }
.hero p { margin: 0; max-width: 680px; color: var(--ink-2); font-size: 17px; }
.formula {
  margin: 22px 0 30px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  font-family: var(--font-cond); font-size: 18px; color: var(--navy);
}
.formula span { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; }
.formula b { color: var(--ink-3); font-weight: 500; }
.formula .out { background: var(--navy); color: #fff; border-color: var(--navy); }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
.card h2 { margin: 0; font-family: var(--font-cond); font-weight: 600; font-size: 22px; color: var(--navy); }
.card p { margin: 0; color: var(--ink-2); }
.card ul { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: 14px; }
.card .actions { margin-top: auto; display: flex; gap: 8px; flex-wrap: wrap; padding-top: 6px; }
.card .tag { align-self: flex-start; font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .6px; padding: 3px 8px; border-radius: 20px; }
.tag.local { background: #FFF5C2; color: #6B5500; }
.tag.shared { background: #D7F2F2; color: #00585B; }
.recent { margin-top: 34px; }
.recent h3 { font-family: var(--font-cond); font-weight: 600; font-size: 18px; color: var(--navy); margin: 0 0 10px; }
.recent-list { display: grid; gap: 8px; }
.recent-item {
  display: flex; align-items: center; gap: 12px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px;
}
.recent-item a { font-weight: 600; text-decoration: none; color: var(--ink); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-item a:hover { color: var(--sea); }
.recent-item small { color: var(--ink-3); white-space: nowrap; }
.empty-note { color: var(--ink-3); font-size: 14px; }

/* Editor */
.editor { padding: 16px 20px 60px; max-width: 1600px; margin: 0 auto; }
.title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.title-input {
  font-family: var(--font-cond); font-weight: 600; font-size: 28px; color: var(--navy);
  border: 1px solid transparent; background: transparent; border-radius: 8px; padding: 2px 8px; margin-left: -8px;
  min-width: 200px; flex: 1 1 360px;
}
.title-input:hover:not([readonly]) { border-color: var(--line); }
.title-input:focus { border-color: var(--sea); background: var(--surface); outline: none; }
.mode-badge { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 20px; white-space: nowrap; }
.mode-badge.local { background: #FFF5C2; color: #6B5500; }
.mode-badge.shared { background: #D7F2F2; color: #00585B; }
.mode-badge.view { background: var(--surface-2); color: var(--ink-2); }
.sync { font-size: 12.5px; color: var(--ink-3); display: inline-flex; align-items: center; gap: 6px; }
.sync::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--viridian); }
.sync.saving::before { background: var(--yellow); }
.sync.error::before { background: var(--coral); }

.context {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 14px;
}
.context label { display: flex; flex-direction: column; gap: 3px; font-size: 11.5px; font-weight: 600; letter-spacing: .5px; text-transform: uppercase; color: var(--ink-3); }
.context label.wide { grid-column: span 2; }
.context input {
  font-size: 15px; font-weight: 500; text-transform: none; letter-spacing: 0; color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; background: var(--surface);
}
.context input:focus { border-color: var(--sea); outline: none; }
.context input[readonly] { border-color: transparent; padding-left: 0; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tab {
  background: none; border: none; border-bottom: 3px solid transparent; padding: 9px 14px; cursor: pointer;
  font-family: var(--font-cond); font-size: 17px; font-weight: 600; color: var(--ink-3); white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--navy); border-bottom-color: var(--viridian); }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.panel-head h3 { margin: 0; font-family: var(--font-cond); font-weight: 600; font-size: 18px; color: var(--navy); }
.panel-head .spacer { flex: 1; }
.panel-head label.check { font-size: 13.5px; color: var(--ink-2); display: inline-flex; gap: 6px; align-items: center; }

/* Matrix table */
.table-wrap { overflow: auto; max-height: calc(100vh - 220px); }
table.matrix { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 14px; }
.matrix td, .matrix th { white-space: normal; }
.matrix th, .matrix td { border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); padding: 0; vertical-align: middle; background: var(--surface); }
.matrix thead th { position: sticky; z-index: 3; background: var(--surface-2); font-weight: 600; color: var(--ink-2); }
.matrix thead tr:nth-child(1) th { top: 0; height: 74px; }
.matrix thead tr:nth-child(2) th { top: 74px; }
.matrix th.sticky-l, .matrix td.sticky-l { position: sticky; z-index: 2; }
.matrix thead th.sticky-l { z-index: 4; }
.matrix .c-no { left: 0; width: 44px; min-width: 44px; text-align: center; color: var(--ink-3); }
.matrix .c-owner { left: 44px; width: 120px; min-width: 120px; }
.matrix .c-text { left: 164px; min-width: 280px; width: 100%; box-shadow: 2px 0 0 var(--line); }
.matrix .c-obj { min-width: 96px; width: 96px; max-width: 120px; text-align: center; }
.matrix .c-calc { min-width: 84px; width: 84px; text-align: right; padding: 0 10px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.matrix .c-ct { min-width: 58px; width: 58px; text-align: center; }
.matrix .c-act { width: 40px; min-width: 40px; text-align: center; border-right: none; }
.matrix th.group { text-transform: uppercase; font-size: 11px; letter-spacing: .6px; }
.matrix th.group-out { background: var(--navy); color: #fff; }
.matrix th.weight-label { text-align: right; padding: 0 10px; font-size: 12px; color: var(--ink-3); font-weight: 500; }

.obj-head { display: flex; flex-direction: column; height: 100%; }
.obj-head textarea {
  border: none; background: transparent; resize: none; text-align: center; font-weight: 600; font-size: 12.5px;
  line-height: 1.2; padding: 6px 4px 2px; flex: 1; color: var(--navy); overflow: hidden;
}
.obj-head textarea:focus { background: #fff; outline: 2px solid var(--focus); outline-offset: -2px; }
.obj-tools { display: flex; justify-content: center; gap: 0; opacity: 0; transition: opacity .15s; }
th:hover .obj-tools, th:focus-within .obj-tools { opacity: 1; }
.obj-tools .icon-btn { width: 22px; height: 20px; font-size: 12px; }

.cell {
  width: 100%; height: 36px; border: none; background: transparent; padding: 0 8px;
  font-size: 14px; color: var(--ink);
}
.cell:focus { background: #fff; outline: 2px solid var(--focus); outline-offset: -2px; position: relative; z-index: 1; }
.cell.num { text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; padding: 0; }
.cell.weight { font-weight: 700; color: var(--navy); }
textarea.cell { resize: none; padding: 8px; line-height: 1.3; height: auto; min-height: 36px; display: block; overflow: hidden; }
.cell[readonly] { cursor: default; }

td.score[data-v="1"], td.score[data-v="2"] { background: #F1FAFA; }
td.score[data-v="3"], td.score[data-v="4"] { background: #D9F1F1; }
td.score[data-v="5"], td.score[data-v="6"] { background: #B4E4E5; }
td.score[data-v="7"] { background: #8FD6D7; }
td.ct[data-v="1"], td.ct[data-v="2"] { background: #FDE3E0; }
td.ct[data-v="3"], td.ct[data-v="4"] { background: #FFF3C4; }
td.ct[data-v="5"], td.ct[data-v="6"] { background: #E3EED0; }
td.ct[data-v="7"] { background: #CFE3B0; }
.invalid { box-shadow: inset 0 0 0 2px var(--coral); }

.overall { font-weight: 700; color: var(--navy); }
.rank-pill {
  display: inline-grid; place-items: center; min-width: 30px; height: 24px; padding: 0 6px; border-radius: 12px;
  font-weight: 700; font-size: 13px; background: var(--surface-2); color: var(--ink-2);
}
.rank-pill.top { background: var(--coral); color: #fff; }
.rank-pill.mid { background: var(--yellow); color: var(--navy); }
.impact-bar { display: block; height: 3px; margin: 3px 0 0 auto; border-radius: 2px; background: var(--viridian); }

tr.add-row td { background: #FAFBFD; }
tr.add-row .cell::placeholder { color: var(--ink-3); }
tr.highlight td { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: #FFF5C2; } }

/* Guide strip */
.guide-strip {
  position: sticky; bottom: 0; z-index: 5; display: flex; gap: 14px; align-items: flex-start; flex-wrap: wrap;
  padding: 10px 14px; border-top: 1px solid var(--line); background: #FBFCFE; font-size: 13px; min-height: 44px;
  border-radius: 0 0 var(--radius) var(--radius);
}
.guide-strip .gtitle { font-family: var(--font-cond); font-weight: 600; font-size: 15px; color: var(--navy); white-space: nowrap; padding-top: 1px; }
.guide-strip ol { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 6px; margin: 0; padding: 0; }
.guide-strip li { padding: 2px 8px; border-radius: 12px; background: var(--surface-2); color: var(--ink-2); cursor: pointer; }
.guide-strip li b { color: var(--navy); margin-right: 4px; }
.guide-strip li.on { background: var(--navy); color: #fff; }
.guide-strip li.on b { color: var(--yellow); }
.guide-strip .hint { color: var(--ink-3); }

/* Ranking */
.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-item { display: grid; grid-template-columns: 52px 1fr auto; gap: 14px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.rank-item:last-child { border-bottom: none; }
.rank-item .r { font-family: var(--font-cond); font-size: 26px; font-weight: 600; color: var(--navy); text-align: center; }
.rank-item .t { font-weight: 600; }
.rank-item .m { font-size: 13px; color: var(--ink-3); margin-top: 2px; }
.rank-item .bar { height: 6px; border-radius: 3px; background: var(--surface-2); margin-top: 8px; overflow: hidden; }
.rank-item .bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--viridian), var(--sea)); }
.rank-item .s { text-align: right; font-variant-numeric: tabular-nums; }
.rank-item .s strong { font-size: 20px; color: var(--navy); display: block; }
.rank-item .s small { color: var(--ink-3); font-size: 12px; }
.quad-tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 10px; margin-left: 6px; vertical-align: 1px; }
.q-now { background: #D7F2F2; color: #00585B; }
.q-plan { background: #DCE6F4; color: var(--sea); }
.q-fill { background: #FFF5C2; color: #6B5500; }
.q-park { background: var(--surface-2); color: var(--ink-3); }
.unscored { padding: 12px 16px; color: var(--ink-3); font-size: 13.5px; border-top: 1px dashed var(--line); }

/* XY chart */
.chart-wrap { padding: 12px 16px 16px; }
.chart-wrap svg { width: 100%; height: auto; display: block; max-height: 70vh; }
.chart-wrap .pt circle { cursor: pointer; transition: r .12s; }
.chart-wrap .pt:hover circle, .chart-wrap .pt.on circle { stroke: var(--navy); stroke-width: 2.5; }
.chart-note { font-size: 13px; color: var(--ink-3); padding: 0 16px 14px; }
.chart-tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--navy); color: #fff; padding: 8px 10px;
  border-radius: 8px; font-size: 13px; max-width: 300px; box-shadow: var(--shadow);
}
.chart-tip b { color: var(--yellow); }

/* Guides */
.guides { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.guide-card .panel-head p { margin: 0; font-size: 13px; color: var(--ink-3); width: 100%; }
.guide-rows { padding: 6px 0; }
.guide-row { display: grid; grid-template-columns: 44px 1fr; align-items: center; border-bottom: 1px solid var(--line); }
.guide-row:last-child { border-bottom: none; }
.guide-row .sc { text-align: center; font-family: var(--font-cond); font-size: 20px; font-weight: 600; color: var(--navy); }
.guide-row textarea { border: none; resize: none; padding: 9px 12px 9px 4px; font-size: 14px; line-height: 1.3; background: transparent; overflow: hidden; }
.guide-row textarea:focus { background: #fff; outline: 2px solid var(--focus); outline-offset: -2px; }
.method { padding: 14px 16px; font-size: 14px; color: var(--ink-2); }
.method ol { margin: 6px 0 0; padding-left: 20px; }
.method li { margin-bottom: 4px; }

/* Dialog + toast */
dialog { border: none; border-radius: 12px; padding: 0; max-width: 560px; width: calc(100% - 32px); box-shadow: 0 20px 60px rgba(27, 33, 64, .3); }
dialog::backdrop { background: rgba(27, 33, 64, .45); }
dialog form { padding: 22px; }
dialog h2 { margin: 0 0 8px; font-family: var(--font-cond); font-weight: 600; color: var(--navy); font-size: 24px; }
dialog p { color: var(--ink-2); margin: 0 0 12px; }
dialog .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
dialog .copy-row { display: flex; gap: 6px; }
dialog .copy-row input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; font-size: 13.5px; background: var(--surface-2); }
dialog .dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
dialog .warn { background: #FFF5C2; color: #5A4700; padding: 9px 11px; border-radius: 8px; font-size: 13.5px; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); font-size: 14px;
}

.notfound { max-width: 560px; margin: 80px auto; text-align: center; padding: 0 20px; }
.notfound h1 { font-family: var(--font-cond); color: var(--navy); }

@media (max-width: 720px) {
  .topbar { padding: 8px 16px; }
  .brand-text small { display: none; }
  .home { padding: 28px 16px 48px; }
  .hero h1 { font-size: 32px; }
  .editor { padding: 12px 16px 48px; }
  .context label.wide { grid-column: auto; }
  .matrix .c-owner { position: static; }
  .matrix .c-text { left: 44px; min-width: 200px; }
  .rank-item { grid-template-columns: 40px 1fr; }
  .rank-item .s { grid-column: 2; text-align: left; }
  .topbar-actions .btn .lbl { display: none; }
  .topbar-actions { flex-wrap: nowrap; gap: 6px; }
  .topbar-actions .btn { padding: 6px 9px; }
  .brand-text strong { font-size: 16px; }
  .table-wrap { max-height: none; }
  .title-input { font-size: 24px; flex-basis: 100%; }
  .matrix .c-owner { display: none; }
  .matrix .c-text { min-width: 180px; }
  .chart-wrap { padding: 8px; }
}

@media print {
  .topbar, .tabs, .panel-head .btn, .guide-strip, .c-act, tr.add-row, .obj-tools { display: none !important; }
  .table-wrap { max-height: none; overflow: visible; }
  body { background: #fff; }
}
