:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --ink: #1c1f2a;
  --ink-soft: #5a6072;
  --border: #e3e6ef;
  --a-color: #2d8b6b;
  --a-bg: #d9efe5;
  --b-color: #2c5fa3;
  --b-bg: #d8e5f5;
  --shared-color: #7a3aa3;
  --shared-bg: #ead8f5;
  --holiday-ring: #c43c3c;
  --vacation-stripe: rgba(255, 196, 0, 0.18);
  --shadow: 0 1px 2px rgba(20, 24, 40, 0.04), 0 4px 14px rgba(20, 24, 40, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.4;
  /* Safe-area-aware so the home indicator and status bar don't collide with content. */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 18px 28px;
  display: flex;
  align-items: center;
  gap: 14px;
}
header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}
header .subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--ink-soft);
}
.settings-toggle {
  display: none;
  background: transparent;
  color: var(--ink);
  font-size: 22px;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 8px;
}
.settings-toggle:hover { background: #eef0f7; }

.header-text { flex: 1; }
.header-help {
  background: #eef0f7;
  color: var(--ink-soft);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  padding: 0;
  font-size: 16px;
  font-weight: 600;
}
.header-help:hover { background: var(--border); color: var(--ink); }

.layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 24px;
  padding: 24px 28px;
  max-width: 1400px;
  margin: 0 auto;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.panel h2 {
  margin: 0 0 12px;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
}
.hint { font-size: 12px; color: var(--ink-soft); margin: 10px 0 0; }

label {
  display: block;
  font-size: 13px;
  margin-bottom: 10px;
}
label > span {
  display: block;
  margin-bottom: 4px;
  color: var(--ink-soft);
}
input[type=text], input[type=date], input[type=number], select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 16px;  /* >= 16px so iOS Safari doesn't auto-zoom on focus */
  background: #fff;
  color: var(--ink);
}
input:focus, select:focus { outline: 2px solid #b9c8e7; outline-offset: -1px; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

button {
  background: var(--ink);
  color: #fff;
  border: none;
  padding: 10px 14px;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
  min-height: 36px;
}
button.full { width: 100%; }
button.secondary { background: #eef0f7; color: var(--ink); }
button.danger { background: #fff; color: #b14040; border: 1px solid #e7c2c2; padding: 5px 10px; min-height: 28px; }
button:hover { filter: brightness(0.95); }

.year-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.year-controls h2 {
  margin: 0;
  font-size: 18px;
  flex: 1;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.month {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  box-shadow: var(--shadow);
}
.month h3 {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 600;
}
.month-header {
  display: grid;
  grid-template-columns: 22px repeat(7, 1fr);
  font-size: 10px;
  color: var(--ink-soft);
  text-transform: uppercase;
  margin-bottom: 4px;
  gap: 2px;
}
.month-header .dow-cell { text-align: center; }
.month-header .week-label {
  text-align: center;
  font-style: italic;
  font-size: 9px;
}
.month-grid {
  display: grid;
  grid-template-columns: 22px repeat(7, 1fr);
  gap: 2px;
}
.week-num {
  font-size: 10px;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.day {
  position: relative;
  aspect-ratio: 1 / 1;
  min-height: 32px;
  border-radius: 5px;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 1px solid transparent;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.day.empty { background: transparent; cursor: default; }
.day.A { background: var(--a-bg); color: var(--a-color); }
.day.B { background: var(--b-bg); color: var(--b-color); }
.day.shared { background: var(--shared-bg); color: var(--shared-color); }
.day.vacation::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 5px;
  background: repeating-linear-gradient(
    45deg,
    transparent 0,
    transparent 4px,
    var(--vacation-stripe) 4px,
    var(--vacation-stripe) 7px
  );
  pointer-events: none;
}
.day.holiday {
  outline: 2px solid var(--holiday-ring);
  outline-offset: -2px;
  font-weight: 700;
}
.day.today { box-shadow: 0 0 0 2px #1c1f2a inset; }
.day:hover:not(.empty) { filter: brightness(0.92); }
.day:active:not(.empty) { filter: brightness(0.85); }

.day.proposed::before {
  content: "?";
  position: absolute;
  top: 1px;
  right: 2px;
  font-size: 9px;
  font-weight: 700;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 1.5px dashed #b14a8a;
  color: #b14a8a;
  z-index: 2;
}
.day.proposed[data-proposed-for="A"]::before { border-color: var(--a-color); color: var(--a-color); }
.day.proposed[data-proposed-for="B"]::before { border-color: var(--b-color); color: var(--b-color); }
.day.proposed[data-proposed-for="shared"]::before { border-color: var(--shared-color); color: var(--shared-color); }

.day.combined {
  flex-direction: column;
  background: #f4f6fb;
  color: var(--ink);
  padding-top: 2px;
  gap: 2px;
}
.day.combined .date-num { font-size: 11px; line-height: 1; }
.day.combined .chips {
  display: flex;
  gap: 2px;
  align-items: center;
  justify-content: center;
}
.day.combined .chip {
  display: inline-block;
  width: 7px;
  height: 4px;
  border-radius: 2px;
}
.day.combined .chip.A { background: var(--a-color); }
.day.combined .chip.B { background: var(--b-color); }
.day.combined .chip.shared { background: var(--shared-color); }
.day.combined .chip.proposed {
  outline: 1px dashed #b14a8a;
  outline-offset: 1px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 12px;
  color: var(--ink-soft);
  margin-bottom: 18px;
}
.legend .swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  vertical-align: middle;
  margin-right: 5px;
}

.summary { font-size: 13px; margin-top: 8px; }
.summary .stat {
  background: #f1f3f8;
  border-radius: 7px;
  padding: 8px;
  text-align: center;
}
.summary .stat strong { display: block; font-size: 16px; }

.overrides-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.overrides-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 7px 0;
  border-top: 1px solid var(--border);
}
.overrides-list li:first-child { border-top: none; }
.overrides-list .meta { color: var(--ink-soft); font-size: 11px; }
.overrides-list li.proposed { background: #fff7e8; border-radius: 6px; padding: 7px 8px; }
.overrides-list .badge {
  display: inline-block;
  background: #f4c95d;
  color: #3a2a00;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 9px;
  margin-left: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.override-actions { display: flex; gap: 4px; }
.override-actions button.accept {
  background: var(--a-color);
  color: #fff;
  border: none;
  padding: 5px 10px;
  font-size: 12px;
  min-height: 28px;
}

/* --- Modal / sheet --------------------------------------------------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.modal.open { display: flex; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 20, 35, 0.45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}
.modal-card {
  position: relative;
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  max-height: calc(100% - 32px);
  overflow-y: auto;
}
.modal-card h3 { margin: 0 0 12px; font-size: 17px; }
.modal-card .actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 14px;
  flex-wrap: wrap;
}
.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  color: var(--ink-soft);
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 22px;
  border-radius: 8px;
  min-height: 36px;
}
.modal-close:hover { background: #eef0f7; }

.day-detail-body {
  font-size: 14px;
  display: grid;
  gap: 8px;
}
.day-detail-body .child-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #f4f6fb;
}
.day-detail-body .child-row.active {
  background: #e6efff;
  outline: 2px solid var(--b-color);
  outline-offset: -2px;
}
.day-detail-body .child-row .name { font-weight: 600; }
.day-detail-body .child-row .value { color: var(--ink); }
.day-detail-body .child-row .meta { color: var(--ink-soft); font-size: 12px; font-weight: 400; }
.day-detail-body .child-row .meta.proposed { color: #b14a8a; }

.day-detail-body .meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  padding: 0 4px;
  margin-top: 4px;
}
.day-detail-body .meta-row .label { color: var(--ink-soft); }
.day-detail-body .meta-row .value { font-weight: 600; }

/* --- Child tabs ----------------------------------------------------------- */

.child-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.child-tab {
  background: #eef0f7;
  color: var(--ink);
  font-size: 13px;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  min-height: 32px;
}
.child-tab.active {
  background: var(--ink);
  color: #fff;
}
.child-tab.add {
  background: transparent;
  color: var(--ink-soft);
  border: 1px dashed var(--border);
}
.child-tab.add:hover { background: #f1f3f8; }

/* --- Summary caption (which child the summary is for) -------------------- */

.summary { display: block; }
.summary-caption {
  font-size: 11px;
  color: var(--ink-soft);
  margin: 0 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

/* Override scope badge */
.overrides-list .badge.scope {
  background: #d8e5f5;
  color: #2c5fa3;
}

/* --- Tooltip (desktop hover only) ------------------------------------------ */

.tooltip {
  position: fixed;
  background: var(--ink);
  color: #fff;
  padding: 6px 9px;
  border-radius: 6px;
  font-size: 12px;
  pointer-events: none;
  z-index: 10;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.1s;
}
.tooltip.show { opacity: 0.95; }
@media (hover: none) or (pointer: coarse) {
  .tooltip { display: none !important; }
}

/* --- Tablet (iPad portrait) ------------------------------------------------ */
@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 280px 1fr;
    padding: 18px 18px;
    gap: 18px;
  }
  .calendar-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* --- Phone ----------------------------------------------------------------- */
@media (max-width: 700px) {
  header { padding: 12px 14px; }
  header h1 { font-size: 17px; }
  header .subtitle { font-size: 11px; }
  .settings-toggle { display: inline-flex; align-items: center; justify-content: center; }

  .layout {
    grid-template-columns: 1fr;
    padding: 14px;
    gap: 14px;
  }
  /* Sidebar becomes a pull-up sheet that overlays the calendar. */
  aside#sidebar {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 40;
    background: var(--surface);
    max-height: 80vh;
    overflow-y: auto;
    padding: 16px 14px max(20px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 30px rgba(0,0,0,0.18);
    transform: translateY(100%);
    transition: transform 0.22s ease-out;
  }
  aside#sidebar.open { transform: translateY(0); }
  aside#sidebar .panel { margin-bottom: 12px; padding: 14px; }

  .calendar-grid { grid-template-columns: 1fr; gap: 10px; }
  .month { padding: 10px; }
  .day { font-size: 12px; }
  .month h3 { font-size: 13px; }

  /* Modal becomes near-full-screen on phone */
  .modal-card { border-radius: 14px; max-width: 100%; padding: 18px; }
}

@media print {
  header, .panel, aside#sidebar, .year-controls button, .legend, .modal { display: none !important; }
  .layout { grid-template-columns: 1fr; padding: 0; }
  body { background: white; }
  .month { break-inside: avoid; box-shadow: none; }
}
