:root {
  --bg: #0b1220;
  --panel: #121a2c;
  --panel-2: #1a253c;
  --border: #243049;
  --text: #e6ecf5;
  --muted: #8a98b3;
  --accent: #4f8cff;
  --accent-2: #6ea8ff;
  --success: #2ecc71;
  --warn: #f1c40f;
  --danger: #ff5566;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 24px rgba(0,0,0,0.35);
  --transition: 220ms ease-out;
  --topbar-h: 56px;
  --bottomnav-h: 64px;
  --gap: 12px;
  --pad: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100vh; height: 100dvh; margin: 0;
  background: var(--bg); color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Pretendard", "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

#app {
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr var(--bottomnav-h);
  height: 100vh; height: 100dvh;
}

/* Top Bar */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; cursor: pointer; }
.logo { font-size: 22px; }
.brand-text { font-size: 16px; letter-spacing: -0.2px; }
.nav { display: none; margin-left: 16px; gap: 4px; flex: 1; }
.nav-btn {
  padding: 8px 14px; border-radius: 999px; color: var(--muted);
  transition: var(--transition);
}
.nav-btn:hover { color: var(--text); background: var(--panel-2); }
.nav-btn.active { color: white; background: var(--accent); }

.fab-add {
  margin-left: auto;
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--accent); color: white; font-size: 22px; font-weight: 700;
  display: grid; place-items: center;
  transition: var(--transition);
  display: none;
}
.fab-add:hover { background: var(--accent-2); }

/* Bottom nav (mobile) */
.bottom-nav {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11px;
  transition: var(--transition);
}
.bottom-nav button.active { color: var(--accent-2); }
.bottom-nav button span { font-size: 20px; line-height: 1; }
.bottom-nav button b { font-weight: 600; font-size: 11px; letter-spacing: -0.2px; }
.bottom-nav .mid span {
  width: 50px; height: 50px; border-radius: 50%;
  background: var(--accent); color: white; display: grid; place-items: center;
  font-size: 28px; font-weight: 600;
  box-shadow: 0 6px 16px rgba(79,140,255,0.4);
  margin-top: -22px;
}
.bottom-nav .mid b { display: none; }

/* View Container */
.view {
  overflow: hidden;
  position: relative;
}

/* ===== Dashboard ===== */
.dash {
  height: 100%;
  overflow-y: auto;
  padding: var(--pad);
  display: grid;
  gap: var(--gap);
  grid-template-columns: 1fr;
  align-content: start;
}
.section-title {
  font-size: 13px; font-weight: 700; color: var(--muted);
  margin: 4px 2px 0; letter-spacing: 0.2px; text-transform: uppercase;
}
.cards-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.card-stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 4px;
  min-height: 70px;
}
.card-stat .label { font-size: 11px; color: var(--muted); }
.card-stat .value { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }
.card-stat .sub { font-size: 11px; color: var(--muted); }
.card-stat.accent { background: linear-gradient(135deg, #2a3e6b, #1a2a4d); border-color: #3a4f80; }
.card-stat.accent .value { color: var(--accent-2); }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex; flex-direction: column;
}
.panel h3 {
  margin: 0 0 10px; font-size: 14px; font-weight: 700;
  display: flex; align-items: center; gap: 6px;
}
.chart-wrap { position: relative; height: 220px; }

.src-table { width: 100%; border-collapse: collapse; }
.src-table th, .src-table td {
  padding: 8px 6px; text-align: center; font-size: 12px;
  border-bottom: 1px solid var(--border);
}
.src-table th { color: var(--muted); font-weight: 600; }
.src-table td.src { text-align: left; font-weight: 600; }
.src-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.src-table tr:last-child td { border-bottom: 0; }
.bar-bg { background: rgba(79,140,255,0.15); border-radius: 4px; height: 4px; margin-top: 4px; overflow: hidden; }
.bar-fg { background: var(--accent); height: 100%; }

/* ===== Records page ===== */
.records-page {
  height: 100%; display: grid;
  grid-template-rows: auto 1fr auto;
  padding: var(--pad); gap: 10px;
}
.records-filters {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
}
.filter-input, .filter-select {
  background: var(--panel); border: 1px solid var(--border);
  color: var(--text); border-radius: var(--radius-sm); padding: 8px 10px;
  font-size: 13px; height: 36px;
}
.btn {
  background: var(--accent); color: white; border-radius: var(--radius-sm);
  padding: 8px 14px; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px; height: 36px;
  transition: var(--transition);
}
.btn:hover { background: var(--accent-2); }
.btn.ghost { background: var(--panel); border: 1px solid var(--border); color: var(--text); }
.btn.ghost:hover { background: var(--panel-2); }
.btn.danger { background: var(--danger); }

.table-wrap {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  display: flex; flex-direction: column; min-height: 0;
}
.table-scroll {
  overflow: auto; flex: 1;
}
table.data {
  width: 100%; border-collapse: collapse;
  table-layout: fixed;
}
table.data thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--panel-2);
  padding: 10px 8px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  text-align: center;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data tbody td {
  padding: 0 8px; font-size: 13px;
  text-align: center; height: 40px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  position: relative;
}
table.data tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data thead th:last-child,
table.data tbody td:last-child { border-right: 0; }
table.data tbody tr:hover { background: rgba(79,140,255,0.06); }

.cell-tip {
  position: absolute; left: 50%; bottom: calc(100% + 4px);
  transform: translateX(-50%);
  background: #1f2a44; color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px; border-radius: 6px;
  font-size: 12px; white-space: pre-wrap; max-width: 280px;
  box-shadow: var(--shadow); z-index: 100;
  pointer-events: none; opacity: 0; transition: opacity 150ms;
  text-align: left;
}
table.data tbody td.has-tip:hover .cell-tip { opacity: 1; }

.row-actions { display: inline-flex; gap: 4px; justify-content: center; }
.icon-btn {
  width: 28px; height: 28px; border-radius: 6px;
  display: grid; place-items: center; color: var(--muted);
  transition: var(--transition);
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }

.pagination {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-top: 1px solid var(--border);
  background: var(--panel-2);
  font-size: 12px; color: var(--muted);
}
.pagination .pages { display: flex; gap: 4px; align-items: center; }
.page-btn {
  min-width: 28px; height: 28px; padding: 0 8px;
  border-radius: 6px; background: transparent;
  color: var(--text); font-size: 12px;
}
.page-btn.active { background: var(--accent); color: white; }
.page-btn:disabled { color: var(--muted); cursor: default; }

/* Settings */
.settings-page {
  height: 100%; overflow-y: auto; padding: var(--pad);
  display: grid; gap: var(--gap);
  grid-template-columns: 1fr;
  align-content: start;
}
.opts-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.opt-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px;
}
.opt-card h3 { margin: 0 0 10px; font-size: 14px; display: flex; justify-content: space-between; }
.opt-list { display: flex; flex-direction: column; gap: 4px; max-height: 240px; overflow-y: auto; }
.opt-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 10px; background: var(--panel-2); border-radius: 6px;
  font-size: 13px;
}
.opt-row button { color: var(--muted); padding: 4px; }
.opt-row button:hover { color: var(--danger); }
.opt-add { display: flex; gap: 6px; margin-top: 8px; }
.opt-add input { flex: 1; background: var(--bg); border: 1px solid var(--border); color: var(--text); border-radius: 6px; padding: 6px 10px; font-size: 13px; }
.opt-add button { background: var(--accent); color: white; padding: 6px 12px; border-radius: 6px; font-size: 12px; }

/* ===== Modal ===== */
.modal-back {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: grid; place-items: center;
  z-index: 100; opacity: 0; transition: opacity 200ms ease-out;
  padding: 20px;
}
.modal-back.open { opacity: 1; }
.modal {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 18px;
  width: min(560px, 100%); max-height: 90vh; overflow-y: auto;
  transform: translateY(20px) scale(0.97);
  transition: transform 220ms ease-out;
  box-shadow: var(--shadow);
}
.modal-back.open .modal { transform: translateY(0) scale(1); }
.modal h2 { margin: 0 0 14px; font-size: 18px; display: flex; justify-content: space-between; align-items: center; }
.modal h2 .close { color: var(--muted); font-size: 24px; line-height: 1; padding: 4px; }

.form-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(2, 1fr);
}
.form-row { display: flex; flex-direction: column; gap: 4px; }
.form-row.full { grid-column: 1 / -1; }
.form-row label { font-size: 12px; color: var(--muted); font-weight: 600; }
.form-row input, .form-row textarea, .form-row select {
  background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 10px 12px;
  font-size: 14px;
}
.form-row textarea { resize: vertical; min-height: 60px; }
.form-row .amount-input { text-align: right; font-variant-numeric: tabular-nums; }

.modal-actions {
  display: flex; gap: 8px; justify-content: flex-end;
  margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border);
}

/* Combobox (search + select) */
.combo { position: relative; }
.combo-input {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 10px 32px 10px 12px;
  font-size: 14px;
}
.combo-arrow {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  color: var(--muted); pointer-events: none;
}
.combo-list {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 8px; margin-top: 4px;
  max-height: 220px; overflow-y: auto;
  z-index: 50;
  box-shadow: var(--shadow);
  display: none;
}
.combo.open .combo-list { display: block; }
.combo-opt {
  padding: 8px 12px; cursor: pointer; font-size: 14px;
}
.combo-opt:hover, .combo-opt.active { background: rgba(79,140,255,0.15); }
.combo-opt.empty { color: var(--muted); cursor: default; }

/* Toast */
.toast-root {
  position: fixed; bottom: calc(var(--bottomnav-h) + 12px); left: 50%;
  transform: translateX(-50%); z-index: 200;
  display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--panel-2); border: 1px solid var(--border);
  color: var(--text); padding: 10px 16px; border-radius: 999px;
  font-size: 13px; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(8px);
  transition: var(--transition);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--danger); }

/* Empty state */
.empty {
  display: grid; place-items: center; height: 200px;
  color: var(--muted); font-size: 13px;
}

/* Skeleton */
.skel {
  background: linear-gradient(90deg, var(--panel) 25%, var(--panel-2) 50%, var(--panel) 75%);
  background-size: 200% 100%;
  animation: skel-shimmer 1.4s infinite;
  border-radius: 6px;
}
@keyframes skel-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ===== Desktop (>= 768px) ===== */
@media (min-width: 768px) {
  :root { --bottomnav-h: 0px; }
  #app { grid-template-rows: var(--topbar-h) 1fr; }
  .bottom-nav { display: none; }
  .nav { display: flex; }
  .fab-add { display: grid; }
  .toast-root { bottom: 24px; }
  .dash {
    grid-template-columns: repeat(2, 1fr);
  }
  .dash .cards-block, .dash .full-row { grid-column: 1 / -1; }
  .cards-row { grid-template-columns: repeat(4, 1fr); }
  .chart-wrap { height: 260px; }
}
@media (min-width: 1100px) {
  .dash { grid-template-columns: repeat(3, 1fr); }
  .dash .cards-block { grid-column: 1 / -1; }
}

/* Mobile fine-tuning */
@media (max-width: 767px) {
  .cards-row { grid-template-columns: repeat(2, 1fr); }
  .brand-text { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  table.data tbody td, table.data thead th { font-size: 12px; padding: 0 6px; }
  .records-filters .filter-input { flex: 1; min-width: 0; }
}
