:root {
  color-scheme: light;

  --page:           #f9f9f7;
  --surface-1:      #fcfcfb;
  --surface-2:      #f3f3f0;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --muted:          #898781;
  --grid:           #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --good:     #0ca30c;
  --warning:  #fab219;
  --serious:  #ec835a;
  --critical: #d03b3b;
  --success-text: #006300;

  --radius: 8px;
  --sidebar-w: 212px;
  --topbar-h: 56px;

  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

/* ---------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--series-1);
  box-shadow: inset 0 0 0 3px var(--surface-1), 0 0 0 1px var(--series-1);
}

.brand-name { font-weight: 650; font-size: 15px; }

.property-picker { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-secondary); }
.property-name { font-weight: 600; font-size: 13.5px; color: var(--text-primary); }
.field select:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
.topbar-spacer { flex: 1; }
.today-label { font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------- layout */

.layout { display: flex; align-items: flex-start; }

.sidebar {
  position: sticky;
  top: var(--topbar-h);
  width: var(--sidebar-w);
  flex: none;
  height: calc(100vh - var(--topbar-h));
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}

.nav-item[hidden] { display: none; }
.nav-item:hover { background: var(--surface-2); color: var(--text-primary); }

.nav-item.is-active {
  background: #eaf2fd;
  color: var(--series-1);
  font-weight: 600;
}

.nav-icon { font-size: 13px; opacity: 0.85; }

.nav-badge {
  margin-left: auto;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--critical);
  color: #fff;
  font-size: 11px;
  font-weight: 650;
  text-align: center;
}

.content { flex: 1; min-width: 0; padding: 24px 28px 64px; }

.screen { display: none; }
.screen.is-active { display: block; }

.screen-head {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 6px;
}

.screen-title { font-size: 20px; }
.screen-actions { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.month-nav { display: flex; align-items: center; gap: 8px; }

.hint { margin: 0 0 16px; font-size: 12.5px; color: var(--muted); }

/* ---------------------------------------------------------- buttons */

.btn-primary, .btn-secondary, .btn-ghost, .btn-danger-ghost {
  border-radius: var(--radius);
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  padding: 7px 14px;
  cursor: pointer;
  border: 1px solid transparent;
}

.btn-primary { background: var(--series-1); color: #fff; }
.btn-primary:hover { background: #256abf; }

.btn-secondary { background: var(--surface-2); color: var(--text-primary); border-color: var(--border); }
.btn-secondary:hover { background: var(--grid); }

.btn-ghost { background: transparent; color: var(--text-secondary); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text-primary); }

.btn-danger-ghost { background: transparent; color: var(--critical); border-color: transparent; }
.btn-danger-ghost:hover { background: #fcecec; }

.btn-icon {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 15px;
  cursor: pointer;
}

.btn-icon:hover { background: var(--surface-2); color: var(--text-primary); }

.btn-mini {
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 550;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  cursor: pointer;
}

.btn-mini:hover { background: var(--surface-2); color: var(--text-primary); }
.btn-mini.is-primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn-mini.is-primary:hover { background: #256abf; }

/* ---------------------------------------------------------- calendar */

.calendar-wrap {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  max-height: calc(100vh - 230px);
}

.cal-grid { display: grid; position: relative; min-width: max-content; }

.cal-cell {
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  height: 42px;
}

.cal-head {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface-1);
  border-bottom: 1px solid var(--baseline);
  font-size: 11px;
  color: var(--text-secondary);
  text-align: center;
  padding: 5px 0;
  height: 46px;
}

.cal-head.is-weekend { background: var(--surface-2); }
.cal-head.is-today { background: #eaf2fd; color: var(--series-1); font-weight: 650; }
.cal-head-dow { display: block; font-size: 10px; color: var(--muted); }
.cal-head.is-today .cal-head-dow { color: var(--series-1); }
.cal-head-num { display: block; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }

.cal-room {
  position: sticky;
  left: 0;
  z-index: 15;
  background: var(--surface-1);
  border-right: 1px solid var(--baseline);
  border-bottom: 1px solid var(--grid);
  padding: 4px 12px;
  height: 42px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
}

.cal-room-no { font-weight: 650; font-size: 13px; line-height: 1.25; }
.cal-room-type {
  font-size: 10.5px;
  line-height: 1.2;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cal-corner {
  position: sticky;
  left: 0;
  top: 0;
  z-index: 25;
  background: var(--surface-1);
  border-right: 1px solid var(--baseline);
  border-bottom: 1px solid var(--baseline);
  height: 46px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  font-size: 11.5px;
  color: var(--muted);
}

.cal-slot { cursor: cell; }
.cal-slot:hover { background: #eaf2fd; }
.cal-slot.is-weekend { background: rgba(0, 0, 0, 0.02); }
.cal-slot.is-weekend:hover { background: #eaf2fd; }

.cal-bar {
  position: absolute;
  height: 30px;
  margin-top: 6px;
  border-radius: 6px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  color: #fff;
  font-size: 11.5px;
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 0 0 2px var(--surface-1);
  z-index: 10;
}

.cal-bar:hover { filter: brightness(0.92); }
.cal-bar.is-cancelled { opacity: 0.4; text-decoration: line-through; }
.cal-bar-name { overflow: hidden; text-overflow: ellipsis; }
.cal-bar-dot {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
}
.cal-bar.is-unpaid .cal-bar-dot { background: var(--warning); box-shadow: 0 0 0 1.5px rgba(255,255,255,0.9); }

.legend { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 14px; font-size: 12px; color: var(--text-secondary); }
.legend-item { display: flex; align-items: center; gap: 7px; }
.legend-swatch { width: 11px; height: 11px; border-radius: 3px; }

/* ---------------------------------------------------------- cards & tables */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 16px;
}

.card-title { font-size: 14px; margin-bottom: 2px; }
.card-sub { font-size: 12px; color: var(--muted); margin-bottom: 14px; }

.board { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }

table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  padding: 0 10px 7px;
  border-bottom: 1px solid var(--baseline);
  white-space: nowrap;
}

td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--grid);
  font-size: 13px;
  vertical-align: middle;
}

tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.empty { padding: 22px 10px; text-align: center; color: var(--muted); font-size: 13px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 550;
  background: var(--surface-2);
  color: var(--text-secondary);
  white-space: nowrap;
}

.chip-dot { width: 7px; height: 7px; border-radius: 50%; }
.chip.is-good { background: #e6f6e6; color: #0a7a0a; }
.chip.is-warning { background: #fef4e0; color: #8a5c00; }
.chip.is-critical { background: #fcecec; color: #a82d2d; }

/* ---------------------------------------------------------- stat tiles */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.tile-label { font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.tile-value { font-size: 23px; font-weight: 650; letter-spacing: -0.02em; line-height: 1.15; }
.tile-unit { font-size: 13px; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.tile-note { font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.tile.is-accent { background: #eaf2fd; border-color: #cde2fb; }
.tile.is-accent .tile-value { color: #1c5cab; }

.tiles-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* ---------------------------------------------------------- charts */

.chart-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.chart-svg { width: 100%; display: block; overflow: visible; }
.chart-bar { cursor: pointer; }
.chart-bar:hover { filter: brightness(0.88); }
.axis-label { font-size: 10.5px; fill: var(--muted); }
.grid-line { stroke: var(--grid); stroke-width: 1; }
.base-line { stroke: var(--baseline); stroke-width: 1; }

.viz-tooltip {
  position: fixed;
  z-index: 90;
  pointer-events: none;
  background: var(--text-primary);
  color: #fff;
  font-size: 12px;
  line-height: 1.45;
  padding: 7px 10px;
  border-radius: 6px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  max-width: 240px;
}

.viz-tooltip b { font-weight: 650; }
.viz-tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; font-variant-numeric: tabular-nums; }

.bar-track { height: 7px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; border-radius: 4px; }

/* ---------------------------------------------------------- forms & modal */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(11, 11, 11, 0.42);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
}

.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--surface-1);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}

.modal-wide { max-width: 760px; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 { font-size: 16px; }
.modal-body { padding: 18px 20px 20px; }

.form-grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 12px; }
.col-4 { grid-column: span 4; }
.col-6 { grid-column: span 6; }
.col-12 { grid-column: span 12; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 11.5px; color: var(--text-secondary); font-weight: 550; }

.field input, .field select, .field-inline input, .field-inline select, .property-picker select {
  font: inherit;
  font-size: 13px;
  padding: 7px 9px;
  border: 1px solid var(--baseline);
  border-radius: 6px;
  background: var(--surface-1);
  color: var(--text-primary);
}

.field input:focus, .field select:focus {
  outline: 2px solid #9ec5f4;
  outline-offset: -1px;
  border-color: var(--series-1);
}

.field-output {
  font-size: 13px;
  padding: 7px 9px;
  border: 1px dashed var(--baseline);
  border-radius: 6px;
  color: var(--text-secondary);
  background: var(--surface-2);
  font-variant-numeric: tabular-nums;
}

.field-inline { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-secondary); }

/* ---------------------------------------------------------- ผู้ใช้และสิทธิ์ */

.who { display: flex; align-items: center; gap: 7px; font-size: 13px; }
.who-name { font-weight: 600; }
.who-role {
  padding: 1px 7px;
  border-radius: 20px;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 550;
}
.who-role.is-owner { background: #eaf2fd; color: #1c5cab; }
.who-role.is-manager { background: #e6f6e6; color: #0a7a0a; }

.perm-block { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--grid); }
.perm-block h3 { font-size: 13px; margin-bottom: 2px; }

.perm-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 6px;
}

.perm-row:hover { background: var(--surface-2); }
.perm-row input { accent-color: var(--series-1); width: 15px; height: 15px; margin: 0; }
.perm-row label { flex: 1; font-size: 13px; cursor: pointer; }
.perm-default { font-size: 11px; color: var(--muted); }
.perm-changed { font-size: 11px; color: var(--serious); font-weight: 600; }

.perm-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.perm-chip {
  padding: 1px 7px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-size: 11px;
}

.user-inactive { opacity: 0.5; }

/* ---------------------------------------------------------- ปิดยอดประจำวัน */

.daily-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 16px;
  align-items: start;
}

.daily-preview { position: sticky; top: calc(var(--topbar-h) + 16px); }
.preview-card { margin-bottom: 0; }
.preview-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }

.preview-text {
  width: 100%;
  height: calc(100vh - 230px);
  min-height: 420px;
  resize: vertical;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11.5px;
  line-height: 1.55;
  padding: 10px 12px;
  border: 1px solid var(--grid);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text-primary);
  white-space: pre;
  overflow: auto;
}

.auto-tag {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  background: #eaf2fd;
  color: #1c5cab;
  font-size: 10.5px;
  font-weight: 600;
}

.save-state { font-size: 12px; color: var(--muted); }
.save-state.is-dirty { color: var(--serious); font-weight: 600; }

.hk-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 8px; }

.hk-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border: 1px solid var(--grid);
  border-radius: var(--radius);
}

.hk-room { font-weight: 650; font-size: 13px; width: 34px; }
.hk-state { font-size: 10.5px; color: var(--muted); flex: 1; }
.hk-buttons { display: flex; gap: 4px; }

.hk-btn {
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 550;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-1);
  color: var(--text-secondary);
  cursor: pointer;
}

.hk-btn:hover { background: var(--surface-2); }
.hk-btn.is-on { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.hk-btn.is-on[data-state="dirty"] { background: var(--serious); border-color: var(--serious); }
.hk-btn.is-on[data-state="ooo"] { background: var(--critical); border-color: var(--critical); }

.repeat-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.repeat-row input { flex: 1; min-width: 0; }

.repeat-row input, .daily-input {
  font: inherit;
  font-size: 13px;
  padding: 6px 9px;
  border: 1px solid var(--baseline);
  border-radius: 6px;
  background: var(--surface-1);
  color: var(--text-primary);
}

.repeat-remove {
  flex: none;
  width: 26px;
  height: 26px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface-1);
  color: var(--muted);
  cursor: pointer;
}

.repeat-remove:hover { background: #fcecec; color: var(--critical); }

.money-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.money-grid .field input { text-align: right; font-variant-numeric: tabular-nums; }

.computed-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 9px 12px;
  margin-top: 10px;
  border-radius: var(--radius);
  background: #eaf2fd;
  font-size: 13px;
}

.computed-line b { font-size: 16px; color: #1c5cab; font-variant-numeric: tabular-nums; }

.auto-list { font-size: 12.5px; color: var(--text-secondary); line-height: 1.9; }
.auto-list .room-tag {
  display: inline-block;
  padding: 1px 7px;
  margin-right: 4px;
  border-radius: 4px;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 12px;
}

.kind-toggle {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.kind-option {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--baseline);
  border-radius: var(--radius);
  font-size: 13.5px;
  font-weight: 550;
  color: var(--text-secondary);
  cursor: pointer;
}

.kind-option input { accent-color: var(--series-1); margin: 0; }
.kind-option:has(input:checked) { border-color: var(--series-1); background: #eaf2fd; color: #1c5cab; }

.tile.is-income .tile-value { color: var(--success-text); }
.tile.is-expense .tile-value { color: var(--critical); }
.tile.is-net { background: #eaf2fd; border-color: #cde2fb; }

.amount-income { color: var(--success-text); font-weight: 600; }
.amount-expense { color: var(--critical); font-weight: 600; }

.category-manager { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px; }
.category-add { display: flex; gap: 8px; margin-top: 10px; }
.category-add input { flex: 1; }

.total-bar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--surface-2);
  border-radius: var(--radius);
}

.total-calc { font-size: 12px; color: var(--text-secondary); line-height: 1.7; }
.field-total { width: 200px; }
.field-total input { font-size: 16px; font-weight: 650; text-align: right; }

.payments-block { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--grid); }
.payments-block h3 { font-size: 13px; margin-bottom: 10px; }
.payment-add { display: flex; align-items: flex-end; gap: 10px; margin-top: 10px; }
.payment-add .field { flex: 1; }

.modal-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--grid);
}

.foot-spacer { flex: 1; }

/* ---------------------------------------------------------- toast */

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 95;
  padding: 10px 18px;
  border-radius: 8px;
  background: var(--text-primary);
  color: #fff;
  font-size: 13px;
  font-weight: 550;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.22);
}

.toast.is-error { background: var(--critical); }
