:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #818cf8;
  --good: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --vac: #a855f7;
  --excl: #64748b;
  --holiday: #f472b6;
  --proposed: #38bdf8;
  --planned: #fbbf24;
  --done: #22c55e;
  --today: #f97316;
  --preferred: rgba(129, 140, 248, 0.18);
}

* { box-sizing: border-box; }

html, body, #root {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 100vh;
  min-height: 100svh;
}

.topbar { display: none; }
.sidebar-overlay { display: none; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  max-height: 100vh;
}

.main {
  padding: 16px 24px;
  overflow-y: auto;
  max-height: 100vh;
}

h1 { font-size: 18px; margin: 0 0 12px 0; }
h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 8px 0; }
h3 { font-size: 14px; margin: 0 0 8px 0; }

.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo { display: block; border-radius: 6px; flex-shrink: 0; }
.topbar-brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field label { color: var(--muted); font-size: 12px; }
.field input[type="number"],
.field input[type="text"],
.field input[type="date"],
.field select,
.field textarea {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
}

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex-shrink: 0; }

button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
button:hover { background: #334155; }
button.primary { background: var(--accent); color: #0b1220; border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: #7dd3fc; }
button.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
button.active { background: var(--accent-2); color: #0b1220; border-color: var(--accent-2); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin: 8px 0 12px;
}
.toolbar-spacer { flex: 1; }

/* Segmented control: pill group of chips */
.seg {
  display: inline-flex;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.chip {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
}
.chip:hover { background: #334155; }
.chip.active {
  background: var(--accent-2);
  color: #0b1220;
  border-color: var(--accent-2);
  font-weight: 600;
}
.chip:disabled { opacity: 0.4; cursor: not-allowed; background: transparent; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  padding: 6px 10px;
  font-size: 16px;
  line-height: 1;
}
.history-group { display: inline-flex; gap: 4px; }

.weekday-pills { display: flex; gap: 4px; flex-wrap: wrap; }
.weekday-pills button { padding: 4px 8px; font-size: 12px; border-radius: 999px; }

.rank-list {
  list-style: none;
  margin: 0 0 8px 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.rank-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 13px;
}
.rank-list .rank {
  color: var(--accent);
  font-weight: 700;
  min-width: 22px;
}
.rank-list .rank-name { flex: 1; }
.rank-list .rank-actions { display: flex; gap: 2px; }
.rank-list .rank-actions button {
  padding: 2px 6px;
  font-size: 12px;
  line-height: 1;
}
.rank-list .rank-empty {
  color: var(--muted);
  font-style: italic;
  justify-content: center;
}

.rank-badge {
  position: absolute;
  right: 4px;
  bottom: 4px;
  font-size: 9px;
  color: var(--accent-2);
  font-weight: 700;
  letter-spacing: .02em;
  pointer-events: none;
}

.stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}
.stat {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}
.stat .label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.stat .value { font-size: 22px; font-weight: 700; margin-top: 2px; }
.stat .value .sub {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  margin-left: 6px;
}
.stat.good .value { color: var(--good); }
.stat.warn .value { color: var(--warn); }
.stat.bad .value { color: var(--bad); }
.stat.planned .value { color: var(--planned); }

.breakdown {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  font-size: 12px;
  color: var(--text);
  margin: 4px 0 12px;
}
.breakdown .muted { color: var(--muted); }

.progress {
  position: relative;
  height: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 16px;
}
.progress > div {
  position: absolute;
  inset: 0 auto 0 0;
  height: 100%;
}
.progress .bar-committed {
  background: linear-gradient(90deg, var(--planned), #f59e0b);
  opacity: .55;
}
.progress .bar-done {
  background: linear-gradient(90deg, var(--done), #16a34a);
}

.months {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}
@media (max-width: 1200px) {
  .months { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
  .months { grid-template-columns: 1fr; }
}
.month {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}
.month-title { font-weight: 600; margin-bottom: 8px; text-align: center; text-transform: capitalize; }
.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.dow { color: var(--muted); font-size: 11px; text-align: center; padding: 4px 0; }
.cell {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  padding: 4px 6px;
  cursor: pointer;
  user-select: none;
  font-size: 12px;
  transition: transform .05s;
}
.cell:hover { border-color: var(--accent); }
.cell.outside { opacity: .25; }
.cell.weekend { background: #1a2336; color: var(--muted); }
.cell.holiday { background: rgba(244, 114, 182, 0.12); border-color: var(--holiday); }
.cell.preferred:not(.weekend):not(.holiday) { background: var(--preferred); }
.cell.range-start { outline: 2px solid var(--accent); }
.cell.in-range { background: rgba(56, 189, 248, 0.15); }
.cell.today {
  border-color: var(--today);
  box-shadow: 0 0 0 2px rgba(249, 115, 22, 0.35) inset;
}
.cell.today .num { color: var(--today); }
.cell .num { font-weight: 600; }
.cell .mark {
  position: absolute;
  left: 4px;
  bottom: 4px;
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 4px;
  font-weight: 700;
  letter-spacing: .03em;
}
.mark.office-done { background: var(--done); color: #052e16; }
.mark.office-planned { background: var(--planned); color: #422006; }
.mark.office-proposed { background: var(--proposed); color: #082f49; }
.mark.vacation { background: var(--vac); color: #2e1065; }
.mark.excluded { background: var(--excl); color: #0f172a; }

.legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin: 8px 0 14px; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }

/* Collapsible legend */
.legend-details { margin: 4px 0 12px; }
.legend-details > summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 0;
  list-style: none;
  user-select: none;
}
.legend-details > summary::-webkit-details-marker { display: none; }
.legend-details > summary::before {
  content: '\25BE';
  display: inline-block;
  margin-right: 6px;
  transition: transform 150ms;
}
.legend-details[open] > summary::before { transform: rotate(180deg); }

/* Mobile compact summary strip (hidden on desktop) */
.summary-strip { display: none; }
.stats-details { display: block; }

.holiday-tooltip {
  position: absolute;
  left: 4px;
  top: 4px;
  width: 6px;
  height: 6px;
  background: var(--holiday);
  border-radius: 50%;
}
.note-dot {
  position: absolute;
  left: 14px;
  top: 1px;
  font-size: 11px;
  color: var(--accent);
  line-height: 1;
  pointer-events: none;
}

.note { color: var(--muted); font-size: 12px; line-height: 1.4; margin: 6px 0; }

.divider { height: 1px; background: var(--border); margin: 14px 0; }

.io-row { display: flex; gap: 6px; }
.io-row input[type="file"] { display: none; }
.label-btn {
  display: inline-block;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
.label-btn:hover { background: #334155; }

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    padding-top: 52px; /* room for fixed topbar */
  }
  .main {
    max-height: none;
    padding: 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .stats { grid-template-columns: repeat(2, 1fr); gap: 6px; margin-bottom: 6px; }
  .stat { padding: 8px; }
  .stat .value { font-size: 18px; }
  .stat .value .sub { display: block; margin-left: 0; margin-top: 1px; font-size: 11px; }
  .stat .label { font-size: 10px; }

  /* Compact summary strip */
  .summary-strip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    width: 100%;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 6px 8px;
    margin: 0 0 6px;
    cursor: pointer;
    color: var(--text);
    min-height: 44px;
  }
  .summary-strip:hover { background: var(--panel); }
  .summary-strip .ss {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
    font-weight: 600;
    flex: 1 1 0;
    justify-content: center;
    min-width: 0;
  }
  .summary-strip .ss-ico {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: 6px;
    font-size: 12px;
    line-height: 1;
    flex-shrink: 0;
  }
  .ss-elig    { background: #475569; color: #e5e7eb; }
  .ss-req     { background: var(--accent-2); color: #0b1220; }
  .ss-done    { background: var(--done);     color: #0b1220; }
  .ss-planned { background: var(--planned);  color: #0b1220; }
  .ss-good    { background: var(--good);     color: #0b1220; }
  .ss-bad     { background: var(--bad);      color: #ffffff; }
  .summary-strip .ss-toggle {
    color: var(--muted);
    font-size: 14px;
    padding: 0 2px;
    flex-shrink: 0;
  }

  /* Hide detailed cards/breakdown by default on mobile; reveal when .open */
  .stats-details { display: none; }
  .stats-details.open { display: block; }
  .stats-details.open .stats { margin-top: 4px; }

  /* Legend closed by default on mobile (controlled by React state). */
  .legend-details { margin: 4px 0 8px; }

  .breakdown { gap: 6px 10px; font-size: 11px; margin: 2px 0 8px; }

  /* Top bar */
  .topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 52px;
    z-index: 30;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    padding: 0 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .topbar-title { font-weight: 700; font-size: 15px; }
  .topbar-window { margin-left: auto; color: var(--muted); font-size: 13px; }

  /* Sidebar becomes a drawer */
  .sidebar {
    position: fixed;
    top: 52px; left: 0; bottom: 0;
    width: min(320px, 88vw);
    max-height: none;
    z-index: 40;
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 200ms ease;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.35);
  }
  .sidebar h1 { display: none; }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 52px 0 0 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 35;
  }

  /* Bigger tap targets */
  button, .label-btn {
    padding: 8px 12px;
    font-size: 14px;
    min-height: 38px;
  }
  .weekday-pills button { padding: 6px 10px; font-size: 13px; min-height: 32px; }
  .rank-list li { padding: 6px 8px; }
  .rank-list .rank-actions button { padding: 6px 10px; font-size: 14px; min-width: 36px; min-height: 32px; }

  /* Inputs: avoid iOS zoom on focus */
  .field input, .field select, .field textarea { font-size: 16px; padding: 8px 10px; }

  /* Toolbar -> wrap into rows, no horizontal scroll */
  .toolbar {
    flex-wrap: wrap;
    overflow-x: visible;
    margin: 6px 0 10px;
    padding: 0;
    gap: 8px;
    row-gap: 8px;
  }
  .toolbar-spacer { display: none; }
  /* "What" segment takes the full first row and distributes chips evenly */
  .toolbar > .seg:first-of-type {
    display: flex;
    flex: 1 1 100%;
    width: 100%;
  }
  .toolbar > .seg:first-of-type .chip {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 4px;
    text-align: center;
    font-size: 12px;
    line-height: 1.15;
    /* Only break at explicit <wbr> points (e.g. Done/<wbr>Planned). */
    white-space: normal;
    word-break: keep-all;
    overflow-wrap: normal;
    hyphens: none;
    border-radius: 16px;
  }
  /* "Mode" segment sits next to the proposal buttons on the second row */
  .toolbar > .seg + .seg { flex-shrink: 0; }
  .toolbar > button {
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
  }
  .chip { padding: 8px 12px; font-size: 13px; min-height: 36px; }
  .seg { padding: 2px; }

  /* Calendar */
  .months { gap: 12px; }
  .month { padding: 8px; }
  .month-title { font-size: 14px; margin-bottom: 6px; }
  .calendar { gap: 3px; }
  .cell {
    aspect-ratio: auto;
    min-height: 42px;
    padding: 2px 4px;
    font-size: 11px;
    border-radius: 5px;
  }
  .cell .num { font-size: 12px; }
  .cell .mark { font-size: 8px; padding: 0 3px; left: 2px; bottom: 2px; }
  .rank-badge { font-size: 8px; right: 2px; bottom: 2px; }
  .note-dot { left: 12px; top: 0; font-size: 10px; }
  .holiday-tooltip { left: 2px; top: 2px; width: 5px; height: 5px; }
  .dow { font-size: 10px; padding: 2px 0; }

  .legend { gap: 6px 10px; font-size: 11px; margin: 4px 0 10px; }
}

@media (max-width: 1200px) and (min-width: 901px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
}

@media (hover: none) {
  .cell:hover { border-color: var(--border); }
  button:hover { background: var(--panel-2); }
}
