/* ============ Blue Sky Ice — design tokens ============ */
:root {
  /* Brand — Carolina Blue accent on neutral surfaces */
  --carolina: #7BAFD4;
  --carolina-deep: #4C7FA5;   /* accessible solid for buttons/badges w/ white text */
  --carolina-ink: #16324A;    /* dark text on Carolina Blue surfaces */
  --carolina-tint: #EAF2F9;   /* pale wash for active states */

  /* Surfaces */
  --bg: #F4F6F8;
  --surface: #FFFFFF;
  --surface-2: #F7F9FB;
  --border: #DCE3EA;

  /* Text */
  --text: #1C2733;
  --text-muted: #5C6B7A;
  --text-faint: #93A1AF;

  /* Semantic */
  --ok: #2E7D32;
  --ok-bg: #E3F2E5;
  --warn: #9A6208;
  --warn-bg: #FBF0DA;
  --danger: #B3261E;
  --danger-bg: #FBE4E2;
  --badge-red: #E5484D;

  /* Type */
  --font-body: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Spacing (4px base) */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 20px; --s6: 24px; --s8: 32px;

  --radius-sm: 8px; --radius-md: 12px; --radius-lg: 16px; --radius-xl: 20px;
  --shadow-sm: 0 1px 2px rgba(22, 50, 74, 0.06);
  --shadow-md: 0 4px 14px rgba(22, 50, 74, 0.09);
  --shadow-lg: 0 12px 32px rgba(22, 50, 74, 0.16);
  --transition: 180ms cubic-bezier(0.16, 1, 0.3, 1);
}

/* ============ Base ============ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
input, select, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--carolina-deep); outline-offset: 2px; border-radius: 4px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ============ Top bar ============ */
.topbar {
  flex: none;
  background: var(--carolina);
  padding-top: env(safe-area-inset-top, 0px);
  box-shadow: var(--shadow-sm);
}
.topbar-inner {
  max-width: 640px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s3) var(--s4);
  min-height: 56px;
}
.brand { display: flex; align-items: center; gap: var(--s2); color: var(--carolina-ink); }
.brand-mark { width: 26px; height: 26px; color: var(--carolina-ink); }
.brand-name { font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.topbar-actions { display: flex; gap: var(--s2); }

.icon-btn {
  position: relative;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  color: var(--carolina-ink);
  border-radius: var(--radius-full, 999px);
  transition: background var(--transition);
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:active { background: rgba(22, 50, 74, 0.12); }

.badge {
  position: absolute; top: 4px; right: 2px;
  min-width: 18px; height: 18px;
  padding: 0 4px;
  background: var(--badge-red); color: #fff;
  font-size: 12px; font-weight: 700; line-height: 18px;
  border-radius: 999px; text-align: center;
  border: 2px solid var(--carolina);
}

/* ============ Main layout ============ */
.main { flex: 1 1 auto; width: 100%; max-width: 640px; margin: 0 auto; padding: var(--s4) var(--s4) var(--s8); overflow-y: auto; -webkit-overflow-scrolling: touch; }

.page-title { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; margin-bottom: var(--s1); }
.page-sub { font-size: 14px; color: var(--text-muted); margin-bottom: var(--s4); }

.section { margin-top: var(--s6); }
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: var(--s3);
}
.section-title { font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.section-count { font-size: 13px; color: var(--text-faint); font-weight: 500; margin-left: var(--s2); }

/* ============ Cards ============ */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--s4);
  margin-bottom: var(--s3);
}
.card + .card { margin-top: var(--s3); }

.card-row { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s3); }
.card-title { font-size: 16px; font-weight: 700; }
.card-meta { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.card-notes { font-size: 13px; color: var(--text-muted); margin-top: var(--s2); }

.card-actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s3); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 700;
  padding: 3px 10px; border-radius: 999px;
  white-space: nowrap;
}
.chip-ok { background: var(--ok-bg); color: var(--ok); }
.chip-warn { background: var(--warn-bg); color: var(--warn); }
.chip-danger { background: var(--danger-bg); color: var(--danger); }

/* ============ Buttons ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s1);
  min-height: 40px; padding: var(--s2) var(--s4);
  border-radius: var(--radius-md);
  font-size: 15px; font-weight: 600;
  transition: background var(--transition), opacity var(--transition);
}
.btn:active { opacity: 0.75; }
.btn-primary { background: var(--carolina-deep); color: #fff; }
.btn-secondary { background: var(--carolina-tint); color: var(--carolina-ink); }
.btn-ghost { background: transparent; color: var(--carolina-deep); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-sm { min-height: 34px; padding: var(--s1) var(--s3); font-size: 13px; }
.btn-block { width: 100%; }

.add-btn {
  width: 100%;
  display: flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 48px;
  border: 1.5px dashed var(--carolina-deep);
  color: var(--carolina-deep);
  border-radius: var(--radius-md);
  font-size: 15px; font-weight: 600;
  background: var(--surface);
  transition: background var(--transition);
}
.add-btn:active { background: var(--carolina-tint); }
.add-btn svg { width: 18px; height: 18px; }

/* ============ Meter reads ============ */
.meter-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s2); margin-bottom: var(--s3); }
.meter-cards .summary-card { padding: var(--s2) var(--s3); }
.meter-cards .summary-value { font-size: 15px; overflow-wrap: anywhere; }
.meter-cards .summary-delta { font-size: 11px; color: var(--text-muted); margin-top: 1px; }
.meter-table { min-width: 430px; }
.meter-table td .meter-delta { display: block; font-size: 11px; color: var(--text-muted); margin-top: 2px; text-align: right; font-variant-numeric: tabular-nums; min-height: 14px; }
.meter-table input { width: 74px; }
.expense-table tr.current-month td { background: var(--carolina-tint); }

/* ============ Service log ============ */
details.svc-log { display: block; }
.svc-summary { display: flex; align-items: center; gap: var(--s2); cursor: pointer; list-style: none; user-select: none; -webkit-tap-highlight-color: transparent; }
.svc-summary::-webkit-details-marker { display: none; }
.svc-summary .section-title { margin: 0; }
.svc-summary .svc-arrow { margin-left: auto; font-size: 18px; line-height: 1; color: var(--text-faint); transition: transform var(--transition); }
details.svc-log[open] > .svc-summary .svc-arrow { transform: rotate(90deg); }
.svc-body { margin-top: var(--s3); }
.svc-body .empty { margin-bottom: var(--s3); }
.card-meta a { color: var(--carolina-deep); font-weight: 600; text-decoration: none; }
.card-meta a:active { text-decoration: underline; }

/* ============ To-do ============ */
.todo-form { display: flex; gap: var(--s2); margin-top: var(--s3); }
.todo-form input[type="text"] {
  flex: 1; min-width: 0; min-height: 44px; padding: var(--s2) var(--s3);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--surface); font-size: 16px;
}
.todo-form input[type="text"]:focus {
  outline: none; border-color: var(--carolina-deep);
  box-shadow: 0 0 0 3px rgba(76, 127, 165, 0.18);
}
.todo-list { margin-top: var(--s2); }
.todo-item { display: flex; align-items: center; gap: var(--s2); padding: 10px 0; border-bottom: 1px solid var(--surface-2); }
.todo-item:last-child { border-bottom: 0; }
.todo-check { position: relative; display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.todo-check input[type="checkbox"] { position: absolute; opacity: 0; width: 0; height: 0; }
.todo-box {
  width: 24px; height: 24px; min-width: 24px; border: 2px solid var(--border); border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  color: transparent; background: var(--surface);
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}
.todo-box svg { width: 13px; height: 13px; }
.todo-check input:checked ~ .todo-box { background: var(--carolina-deep); border-color: var(--carolina-deep); color: #fff; }
.todo-check:active .todo-box { border-color: var(--carolina-deep); }
.todo-text { font-size: 15px; overflow-wrap: anywhere; }
.todo-empty { font-size: 13px; color: var(--text-muted); padding: 12px 0; }

/* ============ Alerts ============ */
.alert-banner {
  display: flex; align-items: center; gap: var(--s3);
  border-radius: var(--radius-md);
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s3);
  font-weight: 600; font-size: 14px;
  cursor: pointer;
  width: 100%; text-align: left;
}
.alert-banner svg { width: 20px; height: 20px; flex-shrink: 0; }
.alert-banner-danger { background: var(--danger-bg); color: var(--danger); }
.alert-banner-warn { background: var(--warn-bg); color: var(--warn); }

.alert-row {
  display: flex; align-items: center; gap: var(--s3);
  width: 100%; text-align: left;
  padding: var(--s3) var(--s2);
  border-radius: var(--radius-md);
  transition: background var(--transition);
}
.alert-row:active { background: var(--surface-2); }
.alert-dot { width: 10px; height: 10px; border-radius: 999px; flex-shrink: 0; }
.alert-dot-danger { background: var(--badge-red); }
.alert-dot-warn { background: #E0A43C; }
.alert-title { font-size: 15px; font-weight: 600; }
.alert-desc { font-size: 13px; color: var(--text-muted); }
.alert-arrow { margin-left: auto; color: var(--text-faint); font-size: 18px; }

/* ============ Tab bar ============ */
.tabbar {
  flex: none;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex; justify-content: center;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.tabbar-inner {
  display: flex;
  max-width: 640px; width: 100%;
}
.tab {
  flex: 1 0 64px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: 56px;
  padding: var(--s1) var(--s2);
  color: var(--text-faint);
  font-size: 11px; font-weight: 600;
  transition: color var(--transition);
}
.tab svg { width: 24px; height: 24px; }
.tab.active { color: var(--carolina-deep); }
.tab.active .tab-indicator { opacity: 1; }
.tab-indicator {
  width: 32px; height: 3px; border-radius: 999px;
  background: var(--carolina-deep);
  opacity: 0; margin-bottom: 1px;
  transition: opacity var(--transition);
}

/* ============ Forms ============ */
.field { margin-bottom: var(--s4); }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--text-muted); margin-bottom: var(--s1); }
.field input, .field select, .field textarea {
  width: 100%;
  min-height: 44px;
  padding: var(--s2) var(--s3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  font-size: 16px;
}
.field textarea { min-height: 72px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--carolina-deep);
  box-shadow: 0 0 0 3px rgba(76, 127, 165, 0.18);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); }

/* ============ Sheet / modal ============ */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(18, 30, 43, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fadeIn 180ms ease-out;
}
.sheet {
  background: var(--surface);
  width: 100%; max-width: 640px;
  max-height: 88dvh;
  overflow-y: auto;
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  padding: var(--s4) var(--s4) calc(var(--s4) + env(safe-area-inset-bottom, 0px));
  animation: slideUp 240ms cubic-bezier(0.16, 1, 0.3, 1);
}
.sheet.grab::before {
  content: ""; display: block;
  width: 40px; height: 4px; border-radius: 999px;
  background: var(--border); margin: 0 auto var(--s4);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--s4); }
.sheet-title { font-size: 19px; font-weight: 800; letter-spacing: -0.01em; }
.sheet-close { font-size: 15px; font-weight: 600; color: var(--carolina-deep); min-height: 44px; padding: 0 var(--s2); }

@keyframes slideUp { from { transform: translateY(60px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ============ Expense table ============ */
.expense-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); background: var(--surface); }
.expense-table { border-collapse: collapse; width: 100%; min-width: 560px; font-size: 14px; }
.expense-table th, .expense-table td { padding: var(--s2) var(--s2); text-align: right; border-bottom: 1px solid var(--border); }
.expense-table th { background: var(--carolina); color: var(--carolina-ink); font-size: 12px; font-weight: 700; white-space: nowrap; text-align: right; }
.expense-table th:first-child, .expense-table td:first-child { text-align: left; position: sticky; left: 0; background: inherit; }
.expense-table th:first-child { background: var(--carolina); }
.expense-table td:first-child { background: var(--surface); font-weight: 600; white-space: nowrap; }
.expense-table tr:last-child td { border-bottom: none; }
.expense-table input {
  width: 64px; min-height: 36px;
  padding: 4px 6px; text-align: right;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: var(--surface-2);
  font-size: 14px;
}
.expense-table input:focus { outline: none; border-color: var(--carolina-deep); background: #fff; }
.expense-table .total-row td { font-weight: 700; background: var(--carolina-tint); }
.expense-table .auto-cell { color: var(--text-muted); }
.expense-table .grand-cell { font-weight: 800; }

.summary-cards { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); margin-bottom: var(--s3); }
.summary-card { background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); padding: var(--s3) var(--s4); }
.summary-label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.summary-value { font-size: 22px; font-weight: 800; letter-spacing: -0.01em; margin-top: 2px; font-variant-numeric: tabular-nums; }

.year-nav { display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s3); }
.year-nav h3 { font-size: 16px; font-weight: 700; }
.year-nav button {
  min-width: 40px; min-height: 36px;
  border-radius: var(--radius-md);
  background: var(--surface); box-shadow: var(--shadow-sm);
  font-size: 16px; font-weight: 700; color: var(--carolina-deep);
}

/* ============ History / details ============ */
details.history { margin-top: var(--s2); }
details.history summary {
  cursor: pointer; list-style: none;
  font-size: 13px; font-weight: 600; color: var(--carolina-deep);
  min-height: 32px; display: flex; align-items: center;
}
details.history summary::-webkit-details-marker { display: none; }
details.history summary::after { content: " ›"; transition: transform var(--transition); }
details.history[open] summary::after { content: " ⌄"; }
.history-list { font-size: 13px; color: var(--text-muted); padding: var(--s2) 0; }
.history-list li { display: flex; justify-content: space-between; padding: var(--s1) 0; border-bottom: 1px solid var(--surface-2); }

/* ============ Empty states ============ */
.empty {
  text-align: center;
  padding: var(--s8) var(--s4);
  color: var(--text-faint);
  font-size: 14px;
}
.empty svg { width: 36px; height: 36px; margin: 0 auto var(--s2); display: block; opacity: 0.5; }

/* ============ Toast ============ */
#toast-root { position: fixed; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); left: 0; right: 0; display: flex; justify-content: center; z-index: 80; pointer-events: none; }
.toast {
  background: var(--carolina-ink); color: #fff;
  font-size: 14px; font-weight: 600;
  padding: var(--s2) var(--s4);
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 200ms ease-out;
}

/* ============ Highlight flash ============ */
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--carolina-deep); } 100% { box-shadow: var(--shadow-sm); } }
.flash { animation: flash 1.4s ease-out 2; }

/* ============ Settings list ============ */
.set-group { background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); overflow: hidden; margin-bottom: var(--s4); }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s3);
  padding: var(--s3) var(--s4);
  min-height: 52px;
  border-bottom: 1px solid var(--surface-2);
  font-size: 15px;
  width: 100%; text-align: left;
}
.set-row:last-child { border-bottom: none; }
.set-row .set-label { font-weight: 600; }
.set-row .set-hint { display: block; font-size: 12px; color: var(--text-faint); font-weight: 400; margin-top: 1px; }

/* ============ Checkbox list (maintenance form) ============ */
.check-list { display: flex; flex-direction: column; gap: 8px; }
.check-row {
  display: flex; align-items: center; gap: 12px;
  min-height: 44px; padding: var(--s2) var(--s3);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--surface); font-size: 15px; cursor: pointer;
}
.check-row input { width: 20px; height: 20px; accent-color: var(--carolina-deep); flex: none; }
.set-row input[type="number"] { width: 72px; min-height: 36px; padding: var(--s1) var(--s2); border: 1px solid var(--border); border-radius: var(--radius-sm); text-align: center; }

@media (min-width: 480px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: var(--radius-xl); margin: var(--s4); }
}
