/* ============================================================
   JM FIN — Allocation-Parity Nendo Minimal Design System
   Exact match to VARP Allocation v4 (Sidebar + Topbar + Stats + Tables + Drawer)
   ============================================================ */

:root {
  color-scheme: light;
  --bg: #F5F5F7;
  --bg-sunken: #EEEEF1;
  --surface: #FFFFFF;
  --surface-hover: #FAFAFA;
  --surface-active: #F0F0F2;

  --ink-1: #1A1A1A;
  --ink-2: rgba(26, 26, 26, 0.72);
  --ink-3: rgba(26, 26, 26, 0.52);
  --ink-4: rgba(26, 26, 26, 0.34);

  --line: rgba(0, 0, 0, 0.06);
  --line-2: rgba(0, 0, 0, 0.11);
  --line-strong: rgba(0, 0, 0, 0.20);

  --good: #5F8C6B;
  --good-bg: rgba(95, 140, 107, 0.10);
  --good-ink: #4E7A59;
  --warn: #C68542;
  --warn-bg: rgba(198, 133, 66, 0.11);
  --warn-ink: #A8652C;
  --bad: #B65555;
  --bad-bg: rgba(182, 85, 85, 0.10);
  --info: #5478A0;
  --info-bg: rgba(84, 120, 160, 0.10);

  --accent: #1A1A1A;
  --accent-hover: #2E2E2E;
  --accent-on: #FFFFFF;
  --focus: rgba(26, 26, 26, 0.18);

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.04), 0 0 0 0.5px rgba(0, 0, 0, 0.05);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.08), 0 0 0 0.5px rgba(0, 0, 0, 0.06);
  --shadow-3: 0 12px 40px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.06);

  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Inter", "IBM Plex Sans Thai", system-ui, sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", Menlo, monospace;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1C1C1E;
  --bg-sunken: #161618;
  --surface: #2C2C2E;
  --surface-hover: #353537;
  --surface-active: #3A3A3C;
  --ink-1: rgba(255, 255, 255, 0.92);
  --ink-2: rgba(255, 255, 255, 0.65);
  --ink-3: rgba(255, 255, 255, 0.45);
  --ink-4: rgba(255, 255, 255, 0.30);
  --line: rgba(255, 255, 255, 0.08);
  --line-2: rgba(255, 255, 255, 0.13);
  --line-strong: rgba(255, 255, 255, 0.22);
  --accent: #FFFFFF;
  --accent-hover: #E5E5E5;
  --accent-on: #1A1A1A;
  --focus: rgba(255, 255, 255, 0.24);
  --good-ink: #7FAF8C;
  --warn-ink: #D99B5C;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.30), 0 0 0 0.5px rgba(255, 255, 255, 0.06);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.36), 0 0 0 0.5px rgba(255, 255, 255, 0.08);
  --shadow-3: 0 12px 40px rgba(0, 0, 0, 0.50), 0 0 0 0.5px rgba(255, 255, 255, 0.10);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: var(--font); font-size: 14px; line-height: 1.5;
  background: var(--bg); color: var(--ink-1);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1;
}
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }
.hidden { display: none !important; }
.n { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
body.privacy-blur .n { filter: blur(8px); user-select: none; }
.n-pos { color: var(--good-ink); }
.n-neg { color: var(--bad); }
.n-warn { color: var(--warn-ink); }
.muted { color: var(--ink-3); }

/* Auth Gate */
.auth-gate {
  position: fixed; inset: 0; z-index: 1000;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: env(safe-area-inset-top, 16px) 16px env(safe-area-inset-bottom, 16px);
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface); border-radius: var(--r-lg);
  padding: 24px 22px; box-shadow: var(--shadow-2); border: 1px solid var(--line);
}
.auth-brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; margin-bottom: 16px; }
.auth-title { font-size: 20px; font-weight: 700; margin: 0 0 4px; letter-spacing: -0.02em; }
.auth-sub { font-size: 13px; color: var(--ink-3); margin: 0 0 16px; }
.pin-dots { display: flex; justify-content: center; gap: 14px; margin: 18px 0; }
.pin-dots span { width: 13px; height: 13px; border-radius: 50%; border: 1.5px solid var(--line-strong); transition: all 0.12s var(--ease); }
.pin-dots span.filled { background: var(--accent); border-color: var(--accent); transform: scale(1.1); }
.numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.numpad button {
  height: 52px; border-radius: var(--r); border: 1px solid var(--line);
  background: var(--bg-sunken); color: var(--ink-1); font-size: 19px; font-weight: 500; touch-action: manipulation;
}
.numpad button:active { background: var(--surface-active); transform: scale(0.97); }
.numpad button.muted { font-size: 13px; color: var(--ink-3); background: transparent; }
.check-row { display: flex; align-items: flex-start; gap: 8px; font-size: 12px; color: var(--ink-2); cursor: pointer; }
.check-row input { margin-top: 3px; }
.err-msg { color: var(--bad); font-size: 12px; margin: 4px 0; text-align: center; }

/* App Shell — Exact Allocation 2-Column Grid */
.app {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  min-height: 100dvh;
}
.sidebar {
  background: var(--bg);
  border-right: 1px solid var(--line);
  padding: 14px 12px calc(12px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column;
  user-select: none;
}
@media (min-width: 881px) {
  .sidebar { position: sticky; top: 0; height: 100vh; z-index: 55; }
}
.brand {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 8px 12px; border-bottom: 1px solid var(--line); margin-bottom: 10px;
}
.brand-title { font-weight: 700; font-size: 15px; letter-spacing: -0.02em; }
.brand-ver { font-size: 10.5px; color: var(--ink-4); font-family: var(--font-mono); margin-left: auto; }
.edge-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 2px rgba(95, 140, 107, 0.22); flex-shrink: 0; }
.app-sidebar-close-btn {
  display: none; margin-left: auto; width: 28px; height: 28px;
  border-radius: var(--r-sm); border: 1px solid transparent; background: transparent; color: var(--ink-2);
  align-items: center; justify-content: center;
}

/* Scope Switcher */
.app-scope-switcher {
  display: flex; gap: 3px; padding: 3px; border-radius: var(--r-sm);
  background: var(--bg-sunken); border: 1px solid var(--line); margin-bottom: 8px;
}
.app-scope-pill {
  flex: 1; height: 28px; border: 0; border-radius: 4px; background: transparent;
  color: var(--ink-3); font-size: 11.5px; font-weight: 600; letter-spacing: 0.02em;
  display: inline-flex; align-items: center; justify-content: center;
}
.app-scope-pill.active { background: var(--surface); color: var(--ink-1); box-shadow: var(--shadow-1); }

/* Sidebar Nav */
.sidebar-nav-scroll { flex: 1; min-height: 0; overflow-y: auto; }
.nav-section {
  font-size: 10.5px; text-transform: lowercase; letter-spacing: 0.08em;
  color: var(--ink-4); padding: 12px 10px 5px; font-weight: 600;
}
.nav-btn {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 7px 10px; border: 0; background: transparent; color: var(--ink-2);
  border-radius: var(--r-sm); font-size: 13.5px; font-weight: 450; text-align: left;
  transition: background 0.15s var(--ease), color 0.15s var(--ease); margin-bottom: 1px;
}
.nav-btn:hover { background: var(--surface-active); color: var(--ink-1); }
.nav-btn.active {
  background: var(--surface-active); color: var(--ink-1); font-weight: 600;
  box-shadow: inset 2.5px 0 0 0 var(--accent), inset 0 0 0 1px var(--line-strong);
}
.nav-btn .ico { width: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center; opacity: 0.72; }
.nav-btn.active .ico { opacity: 1; }
.nav-btn .dot { margin-left: auto; width: 6px; height: 6px; border-radius: 50%; background: var(--warn); opacity: 0; }
.nav-btn.has-alert .dot { opacity: 1; }

.sidebar-footer { margin-top: auto; padding-top: 10px; border-top: 1px solid var(--line); }
.sidebar-footer .user { display: flex; align-items: center; gap: 9px; padding: 6px 8px; }
.user-avatar {
  width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: var(--accent-on);
  font-family: var(--font-mono); font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.user-name { font-size: 12.5px; font-weight: 600; line-height: 1.2; }
.user-sub { font-size: 10.5px; color: var(--ink-3); }
.sidebar-util-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-top: 6px; }
.app-sidebar-util-btn {
  height: 30px; border-radius: var(--r-sm); border: 1px solid var(--line); background: var(--surface);
  color: var(--ink-2); font-size: 11px; font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.app-sidebar-util-btn:hover { background: var(--surface-hover); color: var(--ink-1); border-color: var(--line-2); }
.app-sidebar-util-btn.danger:hover { color: var(--bad); border-color: var(--bad); }

/* Main Content */
.main { padding: 32px 24px 80px; max-width: 1440px; width: 100%; min-width: 0; margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-title h1 { margin: 0; font-size: 25px; font-weight: 700; letter-spacing: -0.022em; line-height: 1.15; }
.page-title .sub { font-size: 13px; color: var(--ink-3); margin-top: 3px; }
.ver-mark { font-family: var(--font-mono); font-size: 11.5px; font-weight: 500; color: var(--ink-3); margin-left: 8px; }
.al-topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Unified 34px Controls (Zero Crooked Alignment) */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-1);
  font-size: 13px; font-weight: 500; line-height: 1; white-space: nowrap;
}
.btn:hover { background: var(--surface-active); border-color: var(--line-strong); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-on); font-weight: 600; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn.full { width: 100%; }

.select-wrap { position: relative; display: inline-flex; align-items: center; }
.nendo-select {
  appearance: none; -webkit-appearance: none;
  height: 34px; padding: 0 28px 0 11px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-1);
  font-size: 13px; font-weight: 500; line-height: 1; cursor: pointer;
}
.select-chev { position: absolute; right: 9px; pointer-events: none; color: var(--ink-3); }

.seg-ctrl {
  display: inline-flex; align-items: center; height: 34px; padding: 2px;
  background: var(--bg-sunken); border: 1px solid var(--line); border-radius: var(--r-sm);
}
.seg-ctrl button {
  height: 28px; padding: 0 10px; border: 0; border-radius: 4px; background: transparent;
  color: var(--ink-3); font-size: 12px; font-weight: 500; white-space: nowrap;
}
.seg-ctrl button.active { background: var(--surface); color: var(--ink-1); box-shadow: var(--shadow-1); font-weight: 600; }

/* Stats & Cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border-radius: var(--r); padding: 14px 16px; box-shadow: var(--shadow-1); }
.stat .label { font-size: 11px; text-transform: uppercase; color: var(--ink-3); letter-spacing: 0.06em; font-weight: 600; }
.stat .val { font-size: 22px; font-weight: 650; margin-top: 4px; letter-spacing: -0.018em; font-variant-numeric: tabular-nums; }
.stat .delta { font-size: 12px; color: var(--ink-3); margin-top: 3px; }
.stat.good .val { color: var(--good-ink); }
.stat.bad .val { color: var(--bad); }
.stat.warn .val { color: var(--warn-ink); }

.card { background: var(--surface); border-radius: var(--r-lg); padding: 18px 20px; box-shadow: var(--shadow-1); margin-bottom: 16px; min-width: 0; max-width: 100%; }
.section-h { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.section-h h2 { margin: 0; font-size: 15.5px; font-weight: 650; letter-spacing: -0.012em; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
@media (max-width: 960px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }

/* Badges */
.b {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 500; background: var(--surface-active); color: var(--ink-2); white-space: nowrap;
}
.b-good { background: var(--good-bg); color: var(--good-ink); }
.b-warn { background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; }
.b-bad { background: var(--bad-bg); color: var(--bad); font-weight: 600; }
.b-info { background: var(--info-bg); color: var(--info); }

/* Allocation-Style Filter Bar & Tables */
.filter-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 12px; background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow-1); margin-bottom: 12px;
}
.search-input {
  height: 34px; flex: 1; min-width: 180px; padding: 0 11px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--bg-sunken); color: var(--ink-1); font-size: 13px;
}
.table-wrap { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow-1); overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th {
  text-align: left; padding: 10px 14px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--ink-3); font-weight: 600; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.tbl td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tbody tr { cursor: pointer; transition: background 0.12s; }
.tbl tbody tr:hover { background: var(--surface-hover); }
.tbl th.num, .tbl td.num { text-align: right; }

.bar-track { height: 7px; border-radius: 99px; background: var(--bg-sunken); overflow: hidden; margin-top: 5px; }
.bar-fill { height: 100%; border-radius: 99px; background: var(--accent); }
.bar-fill.good { background: var(--good); }
.bar-fill.warn { background: var(--warn); }
.bar-fill.bad { background: var(--bad); }

/* Guide Cards (Toy-Simple 2x2 Blocks) */
.guide-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.guide-grid .card { margin-bottom: 0; }
@media (max-width: 960px) { .guide-grid { grid-template-columns: 1fr; } }
.guide-step { display: flex; gap: 12px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--line); }
.guide-step:last-child { border-bottom: 0; }
.step-num {
  width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: var(--accent-on);
  font-family: var(--font-mono); font-size: 12px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.code-chip {
  display: inline-block; font-family: var(--font-mono); font-size: 12px;
  padding: 2px 7px; border-radius: 4px; background: var(--bg-sunken); border: 1px solid var(--line-2);
}

/* Slide-Over Drawer (Allocation Parity) */
.drawer-bg {
  position: fixed; inset: 0; z-index: 200; background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px); opacity: 0; pointer-events: none; transition: opacity 0.2s var(--ease);
}
.drawer-bg.open { opacity: 1; pointer-events: auto; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 210;
  width: min(460px, 100vw); background: var(--surface); box-shadow: var(--shadow-3);
  padding: 22px 20px 60px; overflow-y: auto; transform: translateX(100%); transition: transform 0.24s var(--ease);
}
.drawer.open { transform: translateX(0); }
.drawer-close {
  position: sticky; top: 0; float: right; width: 32px; height: 32px; border-radius: var(--r-sm);
  border: 1px solid var(--line); background: var(--bg-sunken); color: var(--ink-1);
  display: inline-flex; align-items: center; justify-content: center;
}
.stack-sm { display: flex; flex-direction: column; gap: 11px; }
.row-gap { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-2); }
.field input, .field select, .field textarea {
  height: 36px; padding: 0 10px; border-radius: var(--r-sm);
  border: 1px solid var(--line-2); background: var(--bg-sunken); color: var(--ink-1); font-size: 13.5px;
}
.slip-img { width: 100%; max-height: 440px; object-fit: contain; border-radius: var(--r-sm); background: var(--bg-sunken); margin-top: 8px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  z-index: 900; background: var(--ink-1); color: var(--accent-on); max-width: calc(100vw - 32px);
  padding: 9px 16px; border-radius: 99px; font-size: 13px; font-weight: 500; box-shadow: var(--shadow-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Mobile Off-Canvas Sidebar (Allocation <= 880px parity) */
.mobile-nav-toggle {
  display: none; position: fixed; top: 10px; left: 12px; z-index: 120;
  width: 40px; height: 40px; border-radius: var(--r);
  border: 1px solid var(--line-2); background: var(--surface); color: var(--ink-1);
  box-shadow: var(--shadow-1); align-items: center; justify-content: center;
}
.mobile-nav-overlay {
  display: none; position: fixed; inset: 0; z-index: 130; background: rgba(0, 0, 0, 0.35);
}
@media (max-width: 880px) {
  .mobile-nav-toggle { display: inline-flex; }
  body.mobile-nav-open .mobile-nav-toggle,
  body.drawer-open .mobile-nav-toggle { display: none !important; }
  body.mobile-nav-open .mobile-nav-overlay { display: block; }
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 252px; z-index: 140;
    transform: translateX(-105%); transition: transform 0.24s var(--ease); box-shadow: var(--shadow-3);
  }
  body.mobile-nav-open .sidebar { transform: translateX(0); }
  .app-sidebar-close-btn { display: inline-flex; }
  .main { padding: 58px 16px 48px; }
  .topbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-title h1 { font-size: 22px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat .val { font-size: 19px; }
}
