:root {
  --bg: #DCF1FB;
  --bg-soft: #C9E9FA;
  --card: #FFFFFF;
  --ink: #123A52;
  --muted: #7691A1;
  --line: #DCEEF5;
  --line-soft: #E7F4FA;
  --accent: #2F80ED;
  --accent-2: #0EA57D;
  --accent-soft: #D8ECFF;
  --gradient: linear-gradient(135deg, #2F80ED 0%, #1FBF8F 100%);
  --row-head: #EAF7FC;
  --error-bg: #FDE2E2;
  --error-ink: #B3413E;
  --shadow-sm: 0 1px 3px rgba(18, 58, 82, 0.08), 0 1px 2px rgba(18, 58, 82, 0.05);
  --shadow-md: 0 8px 20px rgba(18, 58, 82, 0.12), 0 2px 6px rgba(18, 58, 82, 0.07);
  --radius: 16px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

html {
  overflow-x: hidden;
  width: 100%;
}

body {
  margin: 0;
  font-family: 'Sarabun', sans-serif;
  background: linear-gradient(180deg, var(--bg-soft) 0%, var(--bg) 45%, var(--bg) 100%);
  background-attachment: fixed;
  color: var(--ink);
  overflow-x: hidden;
  width: 100%;
}

a { transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease; }
button { transition: background-color 0.15s ease, opacity 0.15s ease, transform 0.1s ease; }

.page {
  max-width: 1240px;
  width: 100%;
  margin: 0 auto;
  padding: clamp(20px, 3vw, 40px) clamp(16px, 4vw, 36px) 60px;
}

.site-header {
  margin-bottom: 24px;
  padding-bottom: 4px;
}

.eyebrow {
  font-size: 13px;
  color: var(--accent-2);
  font-weight: 700;
  margin-bottom: 4px;
  letter-spacing: 0.1px;
}

.title {
  font-size: clamp(24px, 2.6vw, 30px);
  font-weight: 700;
  margin: 0 0 20px;
  letter-spacing: 0.2px;
  color: var(--ink);
}

.tab-row {
  display: flex;
  gap: 4px;
  background: var(--card);
  padding: 6px;
  border-radius: 999px;
  width: fit-content;
  max-width: 100%;
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
}

.tab-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  color: var(--muted);
  border-radius: 999px;
  white-space: nowrap;
}

.tab-btn svg { width: 17px; height: 17px; flex-shrink: 0; display: none; }

.tab-btn:hover { color: var(--ink); }

.tab-btn.active {
  color: #fff;
  background: var(--gradient);
  box-shadow: var(--shadow-sm);
}

/* ---- bottom tab bar on mobile / tablet ---- */
@media (max-width: 768px) {
  body { padding-bottom: 78px; }

  .tab-row {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 100;
    width: 100%;
    max-width: none;
    background: #FFFFFF;
    border-radius: 22px 22px 0 0;
    box-shadow: 0 -6px 20px rgba(18, 58, 82, 0.12);
    padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
    justify-content: space-around;
    gap: 0;
    overflow-x: visible;
  }

  .tab-btn {
    flex-direction: column;
    gap: 3px;
    padding: 6px 8px;
    border-radius: 14px;
    font-size: 10.5px;
    flex: 1;
  }

  .tab-btn svg { display: block; }

  .tab-btn.active {
    background: transparent;
    color: var(--accent);
  }

  .tab-btn.active svg { color: var(--accent); }

  .nav-auth-link { font-size: 12px; }
}

/* ---- alerts ---- */
.alert-error {
  background: var(--error-bg);
  color: var(--error-ink);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  margin-bottom: 14px;
}

/* ---- form card ---- */
.form-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 24px;
  margin-bottom: 22px;
  box-shadow: var(--shadow-sm);
}

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

@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
}

.field { display: flex; flex-direction: column; gap: 5px; }

.field label {
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 500;
}

.field input,
.field select {
  padding: 9px 12px;
  font-size: 14.5px;
  font-family: 'Sarabun', sans-serif;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #FFFFFF;
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(193, 117, 47, 0.15);
}

.time-input {
  max-width: 110px;
  text-align: center;
  letter-spacing: 0.5px;
  font-variant-numeric: tabular-nums;
}

.add-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--gradient);
  color: #FFFFFF;
  border: none;
  border-radius: 999px;
  padding: 10px 22px;
  font-size: 14.5px;
  font-family: 'Sarabun', sans-serif;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.add-btn:hover { opacity: 0.92; transform: translateY(-1px); }
.add-btn:active { transform: translateY(0); }

/* ---- ledger table ---- */
.ledger {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--card);
  box-shadow: var(--shadow-sm);
}

.ledger-row {
  display: flex;
  align-items: center;
  padding: 13px 18px;
  font-size: 14px;
  border-bottom: 1px solid var(--line-soft);
  transition: background-color 0.12s ease;
}

.ledger-row:nth-child(even):not(.head) { background: rgba(234, 247, 252, 0.6); }
.ledger-row:not(.head):hover { background: var(--row-head); }

.ledger-row.head {
  padding: 11px 18px;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  background: var(--row-head);
  text-transform: none;
}

.ledger-row:last-child { border-bottom: none; }

.ledger-cell {
  padding-right: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.c-teacher { flex: 1.1; font-weight: 600; }
.c-date { flex: 0.95; color: var(--muted); }
.c-place { flex: 1.2; }
.c-driver { flex: 0.9; color: var(--muted); }
.c-note { flex: 1.2; color: var(--muted); }

.cell-sub {
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
}

/* ---- mobile: ledger rows stack into labeled cards instead of cramped columns ---- */
@media (max-width: 680px) {
  .ledger-row.head { display: none; }

  .ledger-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 16px;
  }

  .ledger-cell {
    width: 100%;
    flex: none !important;
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
    padding-right: 0;
  }

  .ledger-cell[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    font-weight: 700;
    color: var(--accent);
    text-transform: none;
    margin-bottom: 3px;
  }

  .row-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .freq-row {
    flex-wrap: wrap;
    row-gap: 6px;
  }
  .freq-name { width: auto; flex: 1 1 55%; }
  .freq-dept { width: auto; flex: 1 1 40%; text-align: right; }
  .freq-bar-track { flex: 1 1 100%; order: 3; }
  .freq-count { width: auto; order: 4; margin-left: auto; }
}

.multi-badge {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 1px 7px;
  white-space: nowrap;
}

.place-count-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  background: var(--gradient);
  border-radius: 999px;
  padding: 3px 12px;
  white-space: nowrap;
}

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

.form-card-title {
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 14px;
}

.form-grid-2 { grid-template-columns: 1fr 1fr; }

.cancel-btn {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  padding: 9px 10px;
}
.cancel-btn:hover { color: var(--ink); }

/* ---- teacher search + grouped checkboxes (visit form) ---- */
.teacher-search-box {
  width: 100%;
  padding: 8px 10px;
  font-size: 13.5px;
  font-family: 'Sarabun', sans-serif;
  border: 1px solid var(--line);
  border-radius: 4px;
  margin: 6px 0 8px;
  outline: none;
}
.teacher-search-box:focus { border-color: var(--accent); }

.teacher-check-groups {
  max-height: 420px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #FFFFFF;
  padding: 4px 12px;
}

.teacher-check-group { padding: 10px 0; border-bottom: 1px solid var(--line); }
.teacher-check-group:last-child { border-bottom: none; }

.teacher-check-group-title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}

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

.teacher-check-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 8px;
}

.teacher-check {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13.5px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  transition: background-color 0.12s ease;
}
.teacher-check:hover { background: var(--row-head); }

.teacher-check input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--accent);
  cursor: pointer;
}

.teacher-check-code {
  font-weight: 700;
  color: var(--accent);
  font-size: 12px;
}

.teacher-check-dept {
  font-size: 11px;
  color: var(--muted);
  margin-left: auto;
}

/* ---- teacher chips (shown wherever multiple teachers on one trip are listed) ---- */
.teacher-chip {
  display: inline-block;
  font-size: 12px;
  font-weight: 500;
  background: var(--row-head);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 9px;
  margin: 2px 4px 2px 0;
  white-space: nowrap;
}

.delete-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 4px;
  font-size: 15px;
  line-height: 1;
}
.delete-btn:hover { background: var(--error-bg); color: var(--error-ink); }

.row-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.edit-btn {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 5px 12px;
  border: 1.5px solid var(--accent);
  border-radius: 999px;
  white-space: nowrap;
}
.edit-btn:hover { background: var(--gradient); color: #fff; border-color: transparent; }

.dept-section-title {
  font-size: 14px;
  font-weight: 700;
  margin: 20px 0 8px;
  color: var(--ink);
}
.dept-section-title:first-of-type { margin-top: 4px; }

.dept-section-count {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
}

.empty-state {
  padding: 28px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* ---- frequency ---- */
.freq-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px 26px;
  box-shadow: var(--shadow-sm);
}

.freq-title { font-size: 15px; font-weight: 600; margin-bottom: 16px; }

.freq-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.freq-rank { width: 20px; font-size: 13px; color: var(--muted); font-weight: 600; }
.freq-name {
  width: 210px;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.freq-bar-track { flex: 1; height: 10px; background: var(--row-head); border-radius: 5px; overflow: hidden; }
.freq-bar-fill { height: 100%; background: var(--gradient); border-radius: 5px; }
.freq-count { width: 62px; text-align: right; font-size: 13px; color: var(--muted); font-weight: 500; }

.freq-dept {
  width: 150px;
  font-size: 12.5px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- page toolbar (print links) ---- */
.page-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
}

/* ---- dashboard ---- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 22px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}

.stat-card-link {
  display: block;
  text-decoration: none;
  color: inherit;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  cursor: pointer;
}

.stat-card-link:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
  border-color: var(--accent);
}

.stat-cta {
  margin-top: 10px;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.15s ease;
}

.stat-card-link:hover .stat-cta { opacity: 1; }

@media (max-width: 640px) {
  .stat-cta { opacity: 1; }
}

.stat-value {
  font-size: 30px;
  font-weight: 700;
  line-height: 1.15;
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.stat-value-sub {
  font-size: 17px;
  font-weight: 600;
  color: var(--muted);
}

.stat-value-date {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.5;
}

.stat-label {
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 4px;
  font-weight: 500;
}

.section-title-inline {
  font-size: 15px;
  font-weight: 700;
}

/* ---- login ---- */
.login-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 26px;
  margin-top: 60px;
  box-shadow: var(--shadow-md);
}

.nav-auth-link {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  padding: 6px 12px;
  white-space: nowrap;
}
.nav-auth-link:hover { color: var(--ink); }

.print-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 16px;
  text-decoration: none;
  box-shadow: var(--shadow-sm);
}
.print-link:hover { background: var(--row-head); }

/* ---- calendar ---- */
.cal-layout {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

@media (max-width: 640px) {
  .cal-layout { flex-direction: column; }
  .cal-card { padding: 14px; }
  .cal-nav { gap: 8px; }
  .cal-month-label { min-width: 0; flex: 1; font-size: 15px; }
  .side-panel { min-width: 0; width: 100%; }
}

.cal-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  flex: 1.5;
  min-width: 0;
  box-shadow: var(--shadow-sm);
}

.cal-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin-bottom: 14px;
}

.nav-btn {
  width: 30px;
  height: 30px;
  border-radius: 5px;
  border: 1px solid var(--line);
  background: #FFFFFF;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-size: 16px;
}

.cal-month-label { font-size: 16px; font-weight: 700; min-width: 170px; text-align: center; }

.cal-dow-row {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  margin-bottom: 4px;
}
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 0; }

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
}

.cal-cell-empty { aspect-ratio: 1; }

.cal-cell {
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #FFFFFF;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4px;
  position: relative;
}

.cal-cell.today { border-color: var(--accent); border-width: 2px; }
.cal-cell.has-visit { cursor: pointer; }
.cal-cell.has-visit:hover { background: #F3EEDF; }
.cal-cell.selected {
  background: var(--accent);
  border-color: var(--accent);
}
.cal-cell.selected .cal-day-num { color: #fff; }
.cal-cell.selected .cal-dot { background: #fff; color: var(--accent); }

.cal-day-num { font-size: 13px; font-weight: 500; }

.cal-dot {
  margin-top: 3px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 50%;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- side panel (day detail) ---- */
.side-panel {
  flex: 1;
  min-width: 240px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #FFFFFF;
  padding: 20px;
  align-self: stretch;
  box-shadow: var(--shadow-sm);
}

.side-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 4px;
}

.side-panel-close {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.side-panel-close:hover { background: var(--row-head); }

.side-panel-count {
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 14px;
}

.side-panel-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.side-visit-card {
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  padding: 10px 12px;
  background: var(--card);
}

.side-visit-teacher { font-size: 14px; font-weight: 600; margin-bottom: 3px; }
.side-visit-place { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.side-visit-teachers { margin-bottom: 4px; }

.side-visit-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.side-visit-index {
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border-radius: 999px;
  padding: 2px 9px;
}

.side-visit-time {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
}

.side-visit-driver {
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 6px;
}

.side-visit-note {
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed var(--line);
  font-style: normal;
}

.side-visit-edit {
  margin-top: 8px;
  text-align: right;
}

.side-panel-empty {
  font-size: 13.5px;
  color: var(--muted);
  text-align: center;
  padding: 30px 6px;
  line-height: 1.7;
}
