:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #faf8f4;
  --border: #e4ded4;
  --border-strong: #cfc6b8;
  --text: #1f2421;
  --muted: #6b6f6a;
  --accent: #2f6f5e;
  --accent-hover: #255a4c;
  --accent-soft: #e3efea;
  --on-accent: #ffffff;
  --warn: #a35a00;
  --warn-soft: #fbefdc;
  --danger: #b3261e;
  --danger-soft: #fbe5e3;
  --today: #2f6f5e;
  --flash: #fff2c6;
  --shadow: 0 1px 2px rgb(31 36 33 / 6%), 0 2px 8px rgb(31 36 33 / 4%);
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141716;
    --surface: #1c201e;
    --surface-2: #222724;
    --border: #2f3532;
    --border-strong: #414945;
    --text: #e8ebe9;
    --muted: #9aa29e;
    --accent: #6cc2a8;
    --accent-hover: #86d3bb;
    --accent-soft: #1f3530;
    --on-accent: #0d1a16;
    --warn: #f0b35c;
    --warn-soft: #3a2c17;
    --danger: #f2877f;
    --danger-soft: #3d1f1d;
    --today: #6cc2a8;
    --flash: #3b3418;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
}

button, input, select { font: inherit; color: inherit; }

.muted { color: var(--muted); }

[hidden] { display: none !important; }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  font-weight: 550;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); border-color: var(--border); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.6; cursor: default; }

.icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:disabled { opacity: 0.35; cursor: default; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
  color: var(--text);
  text-decoration: none;
  margin-right: auto;
}

.fy-nav { display: flex; align-items: center; gap: 6px; }

.fy-nav select {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-weight: 600;
}

.actions { display: flex; gap: 2px; flex-wrap: wrap; }

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

.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px 16px 64px;
}

.caption { margin: 10px 2px 4px; font-size: 13px; }

/* ---------- summary ---------- */

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

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: left;
}
.stat-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.stat-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.stat-sub { font-size: 13px; color: var(--muted); }
.stat-accent { background: var(--accent); border-color: var(--accent); }
.stat-accent .stat-label, .stat-accent .stat-sub, .stat-accent .stat-value { color: var(--on-accent); }
.stat-accent .stat-label, .stat-accent .stat-sub { opacity: 0.85; }
.stat-warn { background: var(--warn-soft); border-color: transparent; cursor: pointer; }
.stat-warn .stat-value, .stat-warn .stat-sub { color: var(--warn); }
.stat-warn:hover { border-color: var(--warn); }
.stat-ok .stat-value { color: var(--accent); }

/* ---------- months & weeks ---------- */

.month { margin-top: 28px; }

.month-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 0 2px 10px;
  font-size: 18px;
  font-weight: 700;
}
.month-total { font-size: 14px; font-weight: 500; color: var(--muted); }
.month-total strong { color: var(--text); font-variant-numeric: tabular-nums; }

.month-weeks {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
}

.week {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  scroll-margin: 90px;
  transition: background-color 0.6s;
}
.week.is-current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), var(--shadow); }
.week.flash { background: var(--flash); }

.week-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 14px 6px;
}
.week-no { font-weight: 700; }
.week-range { font-size: 13px; color: var(--muted); }

.days { display: flex; flex-direction: column; padding: 0 8px; }

.day {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 4px 8px;
  padding: 6px;
  border-radius: var(--radius-sm);
}
.day + .day { border-top: 1px dashed var(--border); border-radius: 0; }
.day.is-future { opacity: 0.6; }
.day.is-future:focus-within { opacity: 1; }
.day.is-missing .hours { border-color: var(--warn); border-style: dashed; }
.day.is-zero .hours { color: var(--muted); }

.day-label { display: flex; align-items: baseline; gap: 6px; cursor: pointer; white-space: nowrap; }
.dow { font-weight: 650; min-width: 30px; }
.dnum { color: var(--muted); font-size: 14px; }
.day.is-extra .dow::after { content: "*"; color: var(--muted); }
.today-tag {
  font-size: 11px;
  font-weight: 700;
  color: var(--on-accent);
  background: var(--today);
  border-radius: 999px;
  padding: 1px 7px;
}

.day-input { display: flex; align-items: center; gap: 4px; }

.hours {
  width: 68px;
  height: 34px;
  font-size: 16px; /* 16px or more stops iPhone Safari zooming in on focus */
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  -moz-appearance: textfield;
}
.hours:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.hours.is-invalid, .day.is-missing .hours.is-invalid {
  border: 2px solid var(--danger);
  background: var(--danger-soft);
  color: var(--danger);
}

.unit { color: var(--muted); font-size: 13px; }

.fill-btn, .note-btn {
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}
.fill-btn { padding: 0 6px; font-weight: 600; }
.fill-btn[hidden] { display: inline-block !important; visibility: hidden; } /* keep columns aligned */
.fill-btn:hover { color: var(--accent); border-color: var(--accent); }
.note-btn { width: 28px; }
.note-btn:hover, .note-btn.has-note { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

.save-state { width: 14px; font-size: 13px; font-weight: 700; text-align: center; }
.save-state[data-status="saving"] { color: var(--muted); }
.save-state[data-status="saved"] { color: var(--accent); }
.save-state[data-status="error"] { color: var(--danger); }

.no-days { margin: 6px 6px 10px; color: var(--muted); font-size: 14px; }

.day-more {
  grid-column: 1 / -1;
  display: flex;
  gap: 6px;
  align-items: center;
}
.day-more[hidden] { display: none; }

.remove-day {
  flex: none;
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: none;
  color: var(--danger);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.remove-day:hover { border-color: var(--danger); background: var(--danger-soft); }

.note {
  flex: 1;
  min-width: 0;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  font-size: 13px;
}
.note:focus { border-color: var(--accent); outline: none; }

.week-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
  padding: 8px 14px 10px;
  border-top: 1px solid var(--border);
  font-size: 14px;
  color: var(--muted);
}
.week-total { margin-right: auto; }
.week-total strong { color: var(--text); font-size: 16px; font-variant-numeric: tabular-nums; }

.add-day {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
}
.add-day:hover { color: var(--accent); background: var(--accent-soft); }

.add-day-picker {
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 13px;
}

.loading, .empty { margin-top: 32px; text-align: center; }

/* ---------- toast & forms ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  font-weight: 550;
  z-index: 20;
}
.toast[data-kind="error"] { background: var(--danger); color: #fff; }

.form-info {
  margin: 0 0 14px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--text);
  font-size: 14px;
}

.form-error {
  margin: 8px 0 0;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 14px;
}

label { font-weight: 600; font-size: 14px; }

input[type="password"], dialog input[type="number"], dialog input[type="text"] {
  display: block;
  width: 100%;
  height: 40px;
  margin: 6px 0 14px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
input[type="password"]:focus, dialog input[type="number"]:focus, dialog input[type="text"]:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---------- settings dialog ---------- */

dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgb(0 0 0 / 25%);
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog h2 { margin: 0 0 14px; font-size: 20px; }
dialog fieldset { border: 0; padding: 0; margin: 0 0 14px; }
dialog legend { font-weight: 600; font-size: 14px; margin-bottom: 6px; }

.weekday-picker { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; max-width: 340px; }
.weekday { min-width: 0; }
.weekday span { width: 100%; min-width: 0 !important; }
.weekday input { position: absolute; opacity: 0; pointer-events: none; }
.weekday span {
  display: inline-block;
  min-width: 46px;
  padding: 6px 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  text-align: center;
  font-weight: 600;
  cursor: pointer;
}
.weekday input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.weekday input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.hint { margin: -6px 0 10px; font-size: 13px; color: var(--muted); }

/* ---------- export & backup dialog ---------- */

dialog.wide { width: min(520px, calc(100vw - 32px)); }
dialog h3 { margin: 0 0 8px; font-size: 15px; }
.data-section { padding: 14px 0; border-top: 1px solid var(--border); }
.data-section .hint { margin: -4px 0 12px; }

.schedule-apply {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 4px;
}
.schedule-apply input[type="number"] { width: 90px; margin: 0; }
.schedule-apply input[type="date"], .schedule-apply select {
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}
.schedule-history-title { margin: 16px 0 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.schedule-history { list-style: none; margin: 0; padding: 0; }
.schedule-history li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-top: 1px dashed var(--border);
  font-size: 14px;
}
.schedule-when { min-width: 150px; color: var(--muted); }
.schedule-days { flex: 1; font-weight: 600; }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; color: var(--danger); }

.btn.outline { border-color: var(--border-strong); color: var(--text); }
.btn.outline:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.export-grid {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
}
.export-label { font-weight: 600; font-size: 14px; }

.backup-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 13px; }

.file-input { max-width: 100%; font-size: 14px; }
.file-input::file-selector-button {
  margin-right: 10px;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.restore-preview {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 14px;
}
.restore-options { border: 0; padding: 0; margin: 12px 0; display: flex; flex-direction: column; gap: 8px; }
.radio { display: flex; gap: 8px; align-items: flex-start; font-weight: 400; cursor: pointer; }
.radio input { margin-top: 3px; accent-color: var(--accent); }
.restore-options + .form-error, #restore-error { margin-bottom: 12px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* ---------- login ---------- */

.login-page {
  min-height: 100vh;
  min-height: 100dvh; /* excludes Safari's toolbars, so no stray scrolling */
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: min(380px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 12px 0 4px; font-size: 22px; }
.login-card p.muted { margin: 0 0 18px; }
.login-card .form-error { margin: -6px 0 14px; }

/* ---------- small screens ---------- */

@media (max-width: 720px) {
  .topbar { position: static; }
  .week { scroll-margin: 16px; }
  .topbar-inner { gap: 8px; }
  .brand { font-size: 16px; }
  .actions { width: 100%; display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
  .actions .btn { padding: 0 4px; font-size: 14px; border-color: var(--border); background: var(--surface); }
  .stat-value { font-size: 24px; }
  .summary { grid-template-columns: repeat(2, 1fr); }
  .summary > .stat-warn, .summary > .stat-ok { grid-column: 1 / -1; }
}

.label-short { display: none; }
@media (max-width: 720px) {
  .label-long { display: none; }
  .label-short { display: inline; }
}

/* Touch screens (iPhone, iPad, Android): 16px text in every field so iOS doesn't
   zoom on focus, and tap targets close to Apple's 44pt guideline. */
@media (pointer: coarse) {
  input, select, textarea, .note, .file-input, .add-day-picker { font-size: 16px; }
  .file-input::file-selector-button { min-height: 44px; padding: 0 14px; }
  button, .btn, a, label, input { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
  .btn, .icon-btn, .fy-nav select { min-height: 44px; }
  .icon-btn { width: 44px; }
  .hours { height: 44px; width: 72px; }
  .fill-btn, .note-btn { height: 40px; min-width: 44px; font-size: 13px; }
  .note, .add-day-picker, .remove-day { height: 40px; }
  .btn.small { min-height: 40px; }
  .add-day { padding: 10px 12px; font-size: 14px; }
  .day { padding: 8px 4px; }
  .weekday span { min-width: 48px; padding: 11px 0; }
  .radio { padding: 6px 0; }
  .radio input { width: 20px; height: 20px; margin-top: 1px; }
}

/* Narrow phones (iPhone SE / mini, or Display Zoom): tighten the day rows. */
@media (max-width: 360px) {
  .container { padding-left: 12px; padding-right: 12px; }
  .days { padding: 0 4px; }
  .day { gap: 4px; }
  .day-input { gap: 3px; }
  .dnum { font-size: 13px; }
  .hours { width: 64px; padding: 0 6px; }
  .fill-btn { padding: 0 4px; min-width: 40px; }
  .note-btn { min-width: 36px; }
  .save-state { width: 10px; }
  .actions .btn { padding: 0 2px; font-size: 13px; }
  .stat { padding: 12px; }
  .stat-value { font-size: 21px; }
}

@media print {
  .topbar, .add-day, .fill-btn, .note-btn, .save-state { display: none !important; }
  .week { break-inside: avoid; box-shadow: none; }
  .day.is-future { opacity: 1; }
}
