:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e4e9;
  --text: #1b1d23;
  --text-muted: #6b7280;
  --primary: #3457d5;
  --primary-soft: #e8edff;
  --success: #16a34a;
  --success-soft: #e8f7ee;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --warning: #d97706;
  --unit-410: #3457d5;
  --unit-1909: #d97706;
  --unit-2110: #16a34a;
  --plat-liveanywhere: #3457d5;
  --plat-33m2: #a855f7;
  --plat-manual: #6b7280;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --surface-2: #23262e;
    --border: #2d313b;
    --text: #e7e9ee;
    --text-muted: #9aa1ac;
    --primary: #7b93ff;
    --primary-soft: #232a4d;
    --success: #4ade80;
    --success-soft: #143523;
    --danger: #f87171;
    --danger-soft: #3a1d1d;
    --warning: #fbbf24;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Pretendard", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app { display: flex; min-height: 100vh; }

/* ---------- Login gate ---------- */
#login-screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--bg); z-index: 100;
}
.login-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 32px; width: 320px; text-align: center;
}
.login-box h1 { font-size: 18px; margin: 0 0 4px; }
.login-box p { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }
.login-box input {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 14px; margin-bottom: 10px;
}
.login-box button {
  width: 100%; padding: 10px; border-radius: 8px; border: none; background: var(--primary);
  color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}
.login-box .error { color: var(--danger); font-size: 12px; margin-top: 8px; min-height: 16px; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 220px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 12px; display: flex; flex-direction: column; gap: 4px;
}
#sidebar .brand { font-weight: 700; font-size: 16px; padding: 0 10px 18px; }
#sidebar .brand span { color: var(--primary); }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  color: var(--text-muted); font-size: 14px; font-weight: 500; cursor: pointer; user-select: none;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); }
#sidebar .spacer { flex: 1; }
#sync-status { font-size: 11px; color: var(--text-muted); padding: 0 12px 8px; }
#sync-status.err { color: var(--danger); }

/* ---------- Main ---------- */
#main { flex: 1; padding: 24px 32px; overflow-y: auto; max-width: 100%; }
.page { display: none; }
.page.active { display: block; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; gap: 12px; }
.page-header h2 { margin: 0; font-size: 20px; }

.btn {
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* ---------- Cards / grid ---------- */
.grid { display: grid; gap: 16px; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px) { .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 4px; font-size: 13px; color: var(--text-muted); font-weight: 600; }
.card .big { font-size: 24px; font-weight: 700; margin-top: 6px; }
.card .sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.unit-tag {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700;
  padding: 2px 8px; border-radius: 999px;
}
.unit-tag.u410 { background: color-mix(in srgb, var(--unit-410) 15%, transparent); color: var(--unit-410); }
.unit-tag.u1909 { background: color-mix(in srgb, var(--unit-1909) 15%, transparent); color: var(--unit-1909); }
.unit-tag.u2110 { background: color-mix(in srgb, var(--unit-2110) 15%, transparent); color: var(--unit-2110); }
.unit-tag.u공통 { background: var(--surface-2); color: var(--text-muted); }

.plat-tag {
  display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600;
  padding: 2px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-muted);
}
.plat-dot { width: 7px; height: 7px; border-radius: 50%; }

/* ---------- Table ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 760px; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; position: sticky; top: 0; background: var(--surface); }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { text-align: right; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.filters select, .filters input {
  padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 13px;
}

/* ---------- Calendar (spreadsheet-style: dates down, units across) ---------- */
.calendar-nav { display: flex; align-items: center; gap: 10px; }
.calendar-nav .month-label { font-weight: 700; font-size: 14px; min-width: 150px; text-align: center; }
.cal-legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-muted); margin-bottom: 14px; }
.cal-legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend-dot { width: 9px; height: 9px; border-radius: 3px; }

.cal-sheet-wrap { max-height: 75vh; overflow-y: auto; }
.cal-sheet { min-width: 560px; }
.cal-sheet th { text-align: center; }
.cal-sheet td.cal-date-cell {
  white-space: nowrap; font-weight: 600; font-size: 12px; color: var(--text-muted);
  background: var(--surface); position: sticky; left: 0;
}
.cal-sheet tr.today-row td.cal-date-cell { color: var(--primary); }
.cal-sheet tr.weekend td.cal-date-cell { color: var(--danger); }
.cal-sheet tr.today-row { background: var(--primary-soft); }
.cal-sheet td.cal-day-cell {
  font-size: 12.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45); display: flex; align-items: center;
  justify-content: center; z-index: 50; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: var(--radius); padding: 22px; width: 480px; max-width: 100%;
  max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal h3 { margin: 0 0 16px; }
.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 4px; font-weight: 600; }
.form-row input, .form-row select, .form-row textarea {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 13px;
}
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ---------- Settlement charts ---------- */
.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 180px; padding: 10px 4px 0; }
.bar-chart .bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 6px; height: 100%; }
.bar-chart .bar { width: 60%; border-radius: 6px 6px 0 0; background: var(--primary); min-height: 2px; }
.bar-chart .bar-val { font-size: 10px; color: var(--text-muted); }
.bar-chart .bar-label { font-size: 11px; color: var(--text-muted); }

.settings-section { max-width: 640px; }
.settings-section h3 { margin-bottom: 6px; }
.settings-section .hint { font-size: 12px; color: var(--text-muted); margin-bottom: 14px; }

.empty-state { text-align: center; color: var(--text-muted); padding: 40px 0; font-size: 13px; }
.toast {
  position: fixed; bottom: 20px; right: 20px; background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; font-size: 13px; z-index: 200; opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }
