/* Kabusen UI — NanoTekno Design System tokens + shared components
   Vanilla-CSS equivalent of the Next.js `components/ui/` library (Brief §"Komponen UI Dipakai Ulang")
   Reference: docs/01-product/05-design-system-brief.md v1.3
*/

/* ── Fonts ─────────────────────────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Sora:wght@600;700;800&family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=IBM+Plex+Mono:wght@400;500;600&display=swap');

/* ── Tokens ────────────────────────────────────────────────────────── */
:root {
  /* Nano Blue — primary */
  --blue-50: #EBF0FF; --blue-100: #CCDAFF; --blue-300: #80A2FF;
  --blue-500: #0246FF; --blue-700: #0031B4; --blue-900: #001C68;
  /* Nano Orange — selective accent */
  --orange-50: #FFF7EB; --orange-100: #FFEBCC; --orange-300: #FFCC80;
  --orange-500: #FE9900; --orange-700: #B26B00; --orange-900: #653D00;
  /* Finance accent (teal-calm, §1a) — selective, not a primary/action color */
  --teal-calm-500: #74C6D9;

  /* Semantic tokens — light (default) */
  --color-bg: #FAFAFA;
  --color-surface: #FFFFFF;
  --color-surface-2: #F2F3F5;
  --color-border: #E5E5E5;
  --color-text-primary: #171717;
  --color-text-secondary: #737373;
  --color-primary: var(--blue-500);
  --color-primary-hover: var(--blue-700);
  --color-primary-contrast: #FFFFFF;
  --color-accent: var(--orange-500);
  --color-finance: var(--teal-calm-500);

  /* Semantic status — universal, not brand tokens */
  --color-good: #16A34A; --color-good-bg: rgba(22,163,74,0.1); --color-good-border: rgba(22,163,74,0.3);
  --color-bad: #DC2626; --color-bad-bg: rgba(220,38,38,0.1); --color-bad-border: rgba(220,38,38,0.3);
  --color-warn: #D97706; --color-warn-bg: rgba(217,119,6,0.1); --color-warn-border: rgba(217,119,6,0.3);

  /* Typography */
  --font-display: 'Sora', system-ui, sans-serif;
  --font-body: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, 'SF Mono', monospace;

  /* Spacing (4pt rhythm) */
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 20px; --space-6: 24px; --space-8: 48px;

  /* Radius */
  --radius-md: 10px; --radius-sm: 7px; --radius-pill: 999px;

  /* Shadow */
  --shadow-sm: 0 1px 3px rgba(23,23,23,0.08), 0 1px 2px rgba(23,23,23,0.06);
  --shadow-lg: 0 20px 40px rgba(23,23,23,0.16), 0 8px 16px rgba(23,23,23,0.08);

  /* Motion */
  --duration-fast: 150ms;
  --duration-base: 250ms;
  --duration-spring: 550ms;
  --ease-standard: cubic-bezier(0.4, 0, 0.2, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  /* iOS safe areas — only non-zero on notch/home-indicator devices when the
     app runs standalone from the home screen with viewport-fit=cover. Every
     element pinned to a screen edge must account for these or it renders
     under the status bar / home indicator. */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Dark mode — system default (no explicit choice) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #0D0D0D;
    --color-surface: #171717;
    --color-surface-2: #1F1F1F;
    --color-border: #262626;
    --color-text-primary: #F2F2F2;
    --color-text-secondary: #8C8C8C;
    --color-primary: #3D71FF;
    --color-primary-hover: #6690FF;
    --color-primary-contrast: #0D0D0D;
    --color-accent: #FFB23D;
    --color-finance: #8DD3E3;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.3);
    --shadow-lg: 0 20px 40px rgba(0,0,0,0.5), 0 8px 16px rgba(0,0,0,0.35);
  }
}
/* Dark mode — explicit user choice (wins over OS either direction) */
:root[data-theme="dark"] {
  --color-bg: #0D0D0D;
  --color-surface: #171717;
  --color-surface-2: #1F1F1F;
  --color-border: #262626;
  --color-text-primary: #F2F2F2;
  --color-text-secondary: #8C8C8C;
  --color-primary: #3D71FF;
  --color-primary-hover: #6690FF;
  --color-primary-contrast: #0D0D0D;
  --color-accent: #FFB23D;
  --color-finance: #8DD3E3;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.3);
  --shadow-lg: 0 20px 40px rgba(0,0,0,0.5), 0 8px 16px rgba(0,0,0,0.35);
}

@media (prefers-reduced-motion: reduce) {
  :root { --duration-fast: 1ms; --duration-base: 1ms; --duration-spring: 1ms; }
}

/* ── Base ──────────────────────────────────────────────────────────── */
body {
  background: var(--color-bg);
  color: var(--color-text-primary);
  font-family: var(--font-body);
  transition: background var(--duration-base) var(--ease-standard), color var(--duration-base) var(--ease-standard);
}
h1, h2, h3, .font-display { font-family: var(--font-display); text-wrap: balance; }
.num, .mono, td.num, .ticker-cell, .rec-ticker, .price, table td:has(> .num) {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ── Elevation ─────────────────────────────────────────────────────── */
.elevated-header {
  background: color-mix(in srgb, var(--color-surface) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--color-border);
}
.elevated-card { box-shadow: var(--shadow-sm); }

/* ── Buttons (vanilla `Button`) ────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  font-family: var(--font-body); font-weight: 700; font-size: 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  padding: 10px 18px;
  min-height: 44px;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard),
              color var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard);
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--color-primary); border-color: var(--color-primary); color: var(--color-primary-contrast); }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); border-color: var(--color-primary-hover); }
.btn-secondary { background: transparent; border-color: var(--color-border); color: var(--color-text-primary); }
.btn-secondary:hover:not(:disabled) { border-color: var(--color-primary); color: var(--color-primary); }
.btn-danger { background: transparent; border-color: var(--color-bad-border); color: var(--color-bad); }
.btn-danger:hover:not(:disabled) { background: var(--color-bad-bg); }
.btn-ghost { background: none; border-color: transparent; color: var(--color-text-secondary); }
.btn-ghost:hover:not(:disabled) { color: var(--color-text-primary); border-color: var(--color-border); }
.btn-sm { padding: 8px 14px; font-size: 13px; min-height: 38px; }
@media (max-width: 639px) {
  .btn-sm { min-height: 44px; }
}

/* ── Badge (tone-mapped, single source of truth) ──────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: var(--radius-pill);
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
  border: 1px solid transparent; white-space: nowrap;
}
.badge-blue   { background: var(--blue-50);   color: var(--blue-700);   border-color: var(--blue-100); }
.badge-orange { background: var(--orange-50); color: var(--orange-700); border-color: var(--orange-100); }
.badge-green  { background: var(--color-good-bg); color: var(--color-good); border-color: var(--color-good-border); }
.badge-red    { background: var(--color-bad-bg);  color: var(--color-bad);  border-color: var(--color-bad-border); }
.badge-gray   { background: var(--color-surface-2); color: var(--color-text-secondary); border-color: var(--color-border); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .badge-blue { background: rgba(61,113,255,0.15); color: #9DB8FF; border-color: rgba(61,113,255,0.3); }
  :root:not([data-theme="light"]) .badge-orange { background: rgba(255,178,61,0.15); color: #FFCE84; border-color: rgba(255,178,61,0.3); }
}
:root[data-theme="dark"] .badge-blue { background: rgba(61,113,255,0.15); color: #9DB8FF; border-color: rgba(61,113,255,0.3); }
:root[data-theme="dark"] .badge-orange { background: rgba(255,178,61,0.15); color: #FFCE84; border-color: rgba(255,178,61,0.3); }

/* Signal/plan tone mapping — the ONE place these map to a badge tone (was duplicated per-page) */
.badge-signal-BUY, .badge-plan-elite { background: var(--color-good-bg); color: var(--color-good); border-color: var(--color-good-border); }
.badge-signal-WATCH, .badge-plan-starter { background: var(--color-warn-bg); color: var(--color-warn); border-color: var(--color-warn-border); }
.badge-signal-HOLD, .badge-plan-trial { background: var(--color-surface-2); color: var(--color-text-secondary); border-color: var(--color-border); }
.badge-plan-pro { background: var(--blue-50); color: var(--blue-700); border-color: var(--blue-100); }
.badge-plan-admin { background: var(--color-bad-bg); color: var(--color-bad); border-color: var(--color-bad-border); }

/* ── Card ──────────────────────────────────────────────────────────── */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}

/* ── Empty state ───────────────────────────────────────────────────── */
.empty-state {
  text-align: center; padding: var(--space-8) var(--space-4);
  color: var(--color-text-secondary);
}
.empty-state .icon { font-size: 32px; margin-bottom: var(--space-3); opacity: 0.7; }
.empty-state p { font-size: 14px; }

/* ── Inputs ────────────────────────────────────────────────────────── */
.input, input[type="text"], input[type="number"], input[type="date"],
input[type="email"], input[type="password"], select {
  font-family: var(--font-body);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-text-primary);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  min-height: 44px;
  transition: border-color var(--duration-fast) var(--ease-standard);
}
.input:focus, input:focus, select:focus { outline: none; border-color: var(--color-primary); }

/* ── Icon toggle (theme/locale switchers) ─────────────────────────────
   Single source of truth — was previously duplicated per-page at 36px
   (admin.html even shrank it further to 32px on mobile), violating the
   44px mobile/tablet touch-target minimum. Desktop may go smaller
   (32-36px) per the breakpoint table since mouse/trackpad precision is
   higher — that's the only place this shrinks now. */
.icon-toggle {
  background: var(--color-surface); border: 1px solid var(--color-border);
  color: var(--color-text-secondary); border-radius: var(--radius-sm);
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 14px; font-weight: 700;
  transition: border-color var(--duration-fast) var(--ease-standard);
  flex-shrink: 0;
}
.icon-toggle:hover { border-color: var(--color-primary); }
@media (min-width: 1024px) {
  .icon-toggle { width: 36px; height: 36px; font-size: 13px; }
}

/* ── Skeleton / shimmer ────────────────────────────────────────────── */
.shimmer-skeleton {
  background: linear-gradient(90deg, var(--color-surface-2) 25%, var(--color-border) 50%, var(--color-surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ── Elevation-glow (one-shot success/attention highlight) ───────────*/
@keyframes elevation-glow {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--color-primary) 45%, transparent); }
  100% { box-shadow: 0 0 0 14px color-mix(in srgb, var(--color-primary) 0%, transparent); }
}
.elevation-glow { animation: elevation-glow var(--duration-base) var(--ease-standard); }

/* ── Orbit Pulse (signature motion — reserved, selective use only) ──*/
.orbit-pulse { position: relative; }
.orbit-pulse::after {
  content: '';
  position: absolute; inset: -4px;
  border-radius: inherit;
  border: 1.5px solid var(--color-primary);
  border-color: var(--color-primary) transparent transparent transparent;
  opacity: 0;
  transform: rotate(-20deg) scale(0.85);
  pointer-events: none;
}
.orbit-pulse.play::after {
  animation: orbit-pulse-sweep var(--duration-spring) var(--ease-spring) 1;
}
@keyframes orbit-pulse-sweep {
  0% { opacity: 0; transform: rotate(-20deg) scale(0.85); }
  35% { opacity: 1; }
  100% { opacity: 0; transform: rotate(70deg) scale(1.08); }
}

/* ── Modal / Sheet (Level 3) ───────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 300;
  display: none; align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 440px;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  padding: var(--space-6);
  /* Bottom sheet sits flush to the screen edge — keep its content clear of
     the iPhone home indicator. */
  padding-bottom: calc(var(--space-6) + var(--safe-bottom));
  animation: sheet-settle var(--duration-spring) var(--ease-spring);
  /* The overlay bottom-aligns the sheet, so a form taller than the viewport
     used to overflow past the top edge with its title unreachable — there is
     no page scroll behind a fixed overlay to recover it. Scroll inside. */
  max-height: calc(100dvh - var(--space-6));
  overflow-y: auto;
  overscroll-behavior: contain;
  /* Without this the max-height above caps only the content box, and the
     padding is added on top — the sheet still ran past the top of the screen. */
  box-sizing: border-box;
}
/* Floating buttons outrank the overlay (z-400 vs z-300), so they used to sit
   on top of an open dialog and could cover its actions. */
body:has(.modal-overlay.open) .wa-float,
body:has(.modal-overlay.open) .ios-install-hint { display: none; }
@keyframes sheet-settle {
  0% { transform: translateY(100%); }
  100% { transform: translateY(0); }
}
@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
  .modal-box { border-radius: var(--radius-md); padding-bottom: var(--space-6); animation: modal-settle var(--duration-spring) var(--ease-spring); }
  @keyframes modal-settle {
    0% { transform: scale(0.92) translateY(12px); opacity: 0; }
    100% { transform: scale(1) translateY(0); opacity: 1; }
  }
}

/* ── WhatsApp floating button ─────────────────────────────────────── */
.wa-float {
  position: fixed; right: var(--space-5); bottom: calc(var(--space-5) + var(--safe-bottom)); z-index: 400;
  width: 52px; height: 52px; border-radius: 50%;
  background: #25D366; color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-lg); text-decoration: none; font-size: 26px;
  transition: transform var(--duration-fast) var(--ease-standard);
}
.wa-float:hover { transform: scale(1.08); }
@media (max-width: 639px) {
  .wa-float { right: var(--space-4); bottom: calc(76px + var(--safe-bottom)); width: 48px; height: 48px; font-size: 22px; }
}

/* ── iOS "Add to Home Screen" hint ───────────────────────────────────
   Anchored to the bottom because that is where Safari's Share button is —
   the banner points at the control the user has to tap. */
.ios-install-hint {
  position: fixed; z-index: 420;
  left: var(--space-4); right: var(--space-4);
  bottom: calc(var(--space-4) + var(--safe-bottom));
  /* On /app the bottom tab bar owns the screen edge; sitting on top of it made
     the whole navigation untappable until the banner was dismissed. */
}
body:has(.app-nav) .ios-install-hint {
  bottom: calc(76px + var(--safe-bottom));
}
@media (min-width: 640px) {
  /* Tablet and up the nav is a sidebar, so the edge is free again. */
  body:has(.app-nav) .ios-install-hint { bottom: calc(var(--space-5) + var(--safe-bottom)); }
}
/* Keep the WhatsApp button clear of the banner while it is showing. */
body:has(.ios-install-hint) .wa-float { bottom: calc(var(--space-5) + 104px + var(--safe-bottom)); }
@media (max-width: 639px) {
  body:has(.ios-install-hint) .wa-float { bottom: calc(76px + 104px + var(--safe-bottom)); }
}
.ios-install-hint {
  display: flex; align-items: flex-start; gap: var(--space-3);
  padding: var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  animation: sheet-settle var(--duration-spring) var(--ease-spring);
}
.ios-install-hint-text { flex: 1; min-width: 0; }
.ios-install-hint-text strong {
  display: block; font-size: 14px; font-weight: 700;
  color: var(--color-text-primary); margin-bottom: 2px;
}
.ios-install-hint-text p {
  margin: 0; font-size: 13px; line-height: 1.5;
  color: var(--color-text-secondary);
}
.ios-install-hint-close {
  flex-shrink: 0; width: 44px; height: 44px; margin: -10px -10px 0 0;
  background: none; border: none; cursor: pointer;
  color: var(--color-text-secondary); font-size: 15px; line-height: 1;
}
@media (min-width: 640px) {
  .ios-install-hint { left: auto; right: var(--space-5); max-width: 360px; }
}

/* ── Responsive table → card list (List/Tabel archetype, mobile) ─────*/
@media (max-width: 767px) {
  .rtable thead { display: none; }
  .rtable, .rtable tbody, .rtable tr, .rtable td { display: block; width: 100%; }
  .rtable tr {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    margin-bottom: var(--space-3);
    padding: var(--space-3) var(--space-4);
    box-shadow: var(--shadow-sm);
    /* This stylesheet has no global border-box reset, so `width:100%` above
       measures the CONTENT box — padding and border then pushed the card 34px
       wider than .table-wrap, which scrolled sideways and clipped the labels
       off the left edge. Keep the padding inside the declared width. */
    box-sizing: border-box;
  }
  .rtable td {
    display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: var(--space-2);
    padding: 6px 0; white-space: normal; text-align: right;
    border: none !important; overflow-wrap: anywhere; min-width: 0;
  }
  .rtable td::before {
    content: attr(data-label);
    font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
    color: var(--color-text-secondary); text-align: left; padding-right: var(--space-3);
  }
  .rtable td:empty, .rtable td[data-label=""] { display: none; }
  /* List/Tabel archetype, mobile: show 2-3 priority fields per card, not every column
     shrunk down — secondary fields are hidden rather than stacked endlessly. */
  .rtable td.rtable-secondary { display: none; }

  /* Action cells are the exception to the empty-label rule above: they carry
     buttons, not a label/value pair, so hiding them stranded the user with a
     card they could not act on at all. Full-width row under the data. */
  .rtable td.rtable-actions {
    display: flex !important;
    justify-content: stretch; gap: var(--space-2);
    margin-top: var(--space-2); padding-top: var(--space-3);
    border-top: 1px solid var(--color-border) !important;
  }
  .rtable td.rtable-actions::before { content: none; }
  .rtable td.rtable-actions .btn { flex: 1; min-height: 44px; }
  /* The icon button keeps its square footprint; the labelled action takes the
     rest of the row rather than both stretching to half the card each. */
  .rtable td.rtable-actions .btn-icon { flex: 0 0 44px; }
}

/* Action cell on tablet/desktop: buttons sit inline in the last column. */
.rtable td.rtable-actions { white-space: nowrap; }
.rtable td.rtable-actions .btn + .btn { margin-left: var(--space-2); }

/* Icon-only button for row actions (edit pencil), sized for touch. */
.btn-icon {
  padding: 0; width: 44px; height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; line-height: 1;
}
@media (min-width: 1024px) {
  .btn-icon { width: 34px; height: 34px; font-size: 14px; }
}
