:root[data-theme="dark"] {
  --bg: #0f1419;
  --bg-elev: #1a2029;
  --bg-elev-2: #232b36;
  --border: #2a3340;
  --text: #e6edf3;
  --text-dim: #8b97a5;
  --accent: #5b9dff;
  --accent-hover: #7eb3ff;
  --danger: #ff6b6b;
  --warn: #ffb84d;
  --success: #4ed99a;
  --busy: #ff6b6b;
  --free: #4ed99a;
  --sleep: #8b6dff;
  --session: #f0a35e;
  --shadow: 0 4px 20px rgba(0,0,0,0.4);
}

:root[data-theme="light"] {
  --bg: #f5f7fa;
  --bg-elev: #ffffff;
  --bg-elev-2: #eef2f7;
  --border: #d8dfe7;
  --text: #1a2029;
  --text-dim: #5b6877;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --warn: #d97706;
  --success: #059669;
  --busy: #ef4444;
  --free: #10b981;
  --sleep: #7c3aed;
  --session: #d97706;
  --shadow: 0 4px 16px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  transition: background-color 0.2s, color 0.2s;
}

#root { min-height: 100vh; display: flex; flex-direction: column; }

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; }

button, .btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.05s;
  font-family: inherit;
}
button:hover, .btn:hover { background: var(--accent-hover); }
button:active, .btn:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

.btn-danger { background: var(--danger); }
.btn-danger:hover { background: var(--danger); filter: brightness(1.15); }

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: none;
  padding: 4px 10px;
}
.btn-ghost:hover { background: var(--bg-elev-2); color: var(--text); }

.btn-sm { padding: 4px 10px; font-size: 12px; }

input, select, textarea {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }

label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 500;
}

.field { margin-bottom: 14px; }

.row { display: flex; gap: 12px; align-items: center; }
.col { display: flex; flex-direction: column; gap: 12px; }
.gap-sm { gap: 6px; }
.gap-lg { gap: 20px; }
.flex-1 { flex: 1; }
.spacer { flex: 1; }

/* Auth screen */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 24px; margin-bottom: 6px; }
.auth-card .subtitle { color: var(--text-dim); margin-bottom: 24px; }
.auth-tabs { display: flex; gap: 4px; margin-bottom: 20px; background: var(--bg-elev-2); padding: 4px; border-radius: 10px; }
.auth-tabs button {
  flex: 1; background: transparent; color: var(--text-dim); padding: 8px;
}
.auth-tabs button.active { background: var(--bg-elev); color: var(--text); box-shadow: var(--shadow); }

.error-msg { color: var(--danger); margin-top: 8px; font-size: 13px; }

/* Layout */
.app-shell {
  display: flex;
  min-height: 100vh;
}
.sidebar {
  width: 260px;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex-shrink: 0;
}
.brand { font-size: 18px; font-weight: 700; color: var(--text); padding: 0 8px; }

.nav-section { display: flex; flex-direction: column; gap: 2px; }
.nav-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  padding: 8px 8px 4px;
  font-weight: 600;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  font-family: inherit;
}
.nav-item:hover { background: var(--bg-elev-2); }
.nav-item.active { background: var(--accent); color: white; }

.main {
  flex: 1;
  padding: 28px 36px;
  overflow-x: auto;
  min-width: 0;
}
.main > * { max-width: 1600px; margin-left: auto; margin-right: auto; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}
.page-title { font-size: 22px; font-weight: 700; }
.page-sub { color: var(--text-dim); font-size: 13px; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}
.card h3 { font-size: 16px; margin-bottom: 12px; }

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

.list { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  background: var(--bg-elev-2);
  padding: 12px 14px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.list-item .name { font-weight: 500; }
.list-item .meta { color: var(--text-dim); font-size: 12px; }

.badge {
  background: var(--bg-elev-2);
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
}
.badge-accent { background: var(--accent); color: white; }
.badge-warn { background: var(--warn); color: white; }
.badge-success { background: var(--success); color: white; }
.badge-danger { background: var(--danger); color: white; }

.code-pill {
  background: var(--bg-elev-2);
  border: 1px dashed var(--border);
  padding: 6px 12px;
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.1em;
}

/* Schedule grid */
.schedule-toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.mode-toggle {
  display: inline-flex;
  background: var(--bg-elev-2);
  padding: 3px;
  border-radius: 8px;
  gap: 2px;
}
.mode-toggle button {
  background: transparent;
  color: var(--text-dim);
  padding: 6px 12px;
  font-size: 13px;
}
.mode-toggle button.active {
  background: var(--bg-elev);
  color: var(--text);
}
.mode-toggle button.active.busy { color: var(--busy); }
.mode-toggle button.active.free { color: var(--free); }

.schedule-grid {
  display: grid;
  grid-template-columns: 48px repeat(7, minmax(56px, 80px));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  width: max-content;
  max-width: 100%;
}
.sg-head {
  background: var(--bg-elev);
  padding: 8px 4px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}
.sg-time {
  background: var(--bg-elev);
  padding: 0 6px;
  text-align: right;
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  font-variant-numeric: tabular-nums;
}
.sg-cell {
  background: var(--bg-elev);
  height: 22px;
  cursor: pointer;
  position: relative;
}
.sg-cell.busy { background: var(--busy); }
.sg-cell.free { background: var(--free); }
.sg-cell.sleep { background: var(--sleep); opacity: 0.6; }
.sg-cell:hover { outline: 1px solid var(--accent); z-index: 1; }
.sg-cell.preview {
  outline: 2px solid var(--accent);
  z-index: 2;
  filter: brightness(1.15);
}
.sg-cell.has-session::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--session-color, var(--session));
  clip-path: polygon(100% 0, 0 100%, 100% 100%);
  pointer-events: none;
}

/* Session calendar (graphical, read-only) */
.session-calendar {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-elev);
  width: max-content;
  max-width: 100%;
}
.sc-header-row {
  display: grid;
  grid-template-columns: 48px repeat(7, minmax(72px, 110px));
  background: var(--bg-elev-2);
  border-bottom: 1px solid var(--border);
}
.sc-head {
  padding: 8px 4px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}
.sc-head.today { color: var(--accent); }
.sc-head .sc-date { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.sc-body {
  display: grid;
  grid-template-columns: 48px repeat(7, minmax(72px, 110px));
}
.sc-time-col, .sc-day-col {
  position: relative;
  border-right: 1px solid var(--border);
}
.sc-day-col:last-child { border-right: none; }
.sc-cell {
  height: 22px;
  box-sizing: border-box;
  border-top: 1px solid var(--border);
  position: relative;
}
.sc-day-col .sc-cell:first-child,
.sc-time-col .sc-cell:first-child { border-top: none; }
.sc-time-label {
  position: absolute;
  right: 6px;
  top: 2px;
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.sc-event {
  position: absolute;
  left: 2px;
  right: 2px;
  background: var(--ev-color, var(--session));
  color: white;
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 11px;
  line-height: 1.25;
  overflow: hidden;
  cursor: pointer;
  z-index: 2;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: filter 0.1s;
}
.sc-event:hover { filter: brightness(1.1); z-index: 3; }
.sc-event-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sc-event-meta { font-size: 10px; opacity: 0.9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sc-now-line {
  position: absolute;
  left: 0; right: 0;
  height: 2px;
  background: var(--accent);
  z-index: 4;
  pointer-events: none;
}
.sc-now-line::before {
  content: '';
  position: absolute;
  left: -4px; top: -3px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--text-dim); }
.legend-item { display: flex; align-items: center; gap: 6px; }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; }

/* Sessions */
.session-card {
  background: var(--bg-elev-2);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.session-time {
  color: var(--text-dim);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.session-title { font-weight: 500; }

.proposal-list { display: flex; flex-direction: column; gap: 8px; max-height: 480px; overflow-y: auto; }
.proposal {
  background: var(--bg-elev-2);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.proposal.warn { border-left: 3px solid var(--warn); }
.proposal-time { font-variant-numeric: tabular-nums; flex: 1; }
.proposal-warning { font-size: 12px; color: var(--warn); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  background: var(--bg-elev);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal h2 { margin-bottom: 16px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-dim);
}

.theme-toggle {
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 10px;
  font-size: 12px;
}

.support-pill {
  position: fixed;
  right: 16px;
  bottom: 16px;
  background: #f97316;
  color: #fff;
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  box-shadow: 0 4px 14px rgba(249, 115, 22, 0.45);
  z-index: 90;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
}
.support-pill:hover {
  background: #ea580c;
  color: #fff;
  text-decoration: none;
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(249, 115, 22, 0.55);
}
.support-pill:active { transform: translateY(0); }
@media (max-width: 600px) {
  .support-pill { right: 10px; bottom: 10px; padding: 7px 11px; font-size: 12px; }
}

/* Sleep config */
.time-input-row { display: flex; gap: 10px; align-items: center; }
.time-input-row input[type=time] { width: auto; min-width: 110px; }

/* ==== Responsive ==== */

/* Tablet & smaller: collapse sidebar to top, lay nav horizontally */
@media (max-width: 900px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 12px;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
  }
  .sidebar .brand { padding: 0 6px 0 0; font-size: 16px; flex: 0 0 auto; }
  .sidebar .spacer { display: none; }
  .nav-section {
    flex: 1 1 100%;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 4px;
  }
  .nav-title {
    width: 100%;
    padding: 6px 4px 0;
    font-size: 10px;
  }
  .nav-item {
    width: auto;
    flex: 0 0 auto;
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 999px;
    background: var(--bg-elev-2);
  }
  .nav-item.active { background: var(--accent); }
  .main { padding: 18px 16px; }
}

/* Phone */
@media (max-width: 600px) {
  body { font-size: 13px; }
  .main { padding: 14px 12px; }
  .card { padding: 14px; margin-bottom: 12px; border-radius: 10px; }
  .auth-card { padding: 22px 18px; border-radius: 12px; }
  .auth-card h1 { font-size: 22px; }
  .grid-2 { grid-template-columns: 1fr; gap: 12px; }
  .page-title { font-size: 18px; }
  .card h3 { font-size: 15px; }

  .schedule-grid {
    grid-template-columns: 36px repeat(7, minmax(34px, 1fr));
    width: 100%;
  }
  .sg-time { padding: 0 3px; font-size: 10px; }
  .sg-cell { height: 20px; }

  .session-calendar {
    width: 100%;
  }
  .session-calendar .sc-header-row,
  .session-calendar .sc-body {
    grid-template-columns: 36px repeat(7, minmax(50px, 1fr));
  }
  .sc-cell { height: 20px; }
  .sc-time-label { right: 3px; font-size: 10px; }
  .sc-head { padding: 6px 2px; font-size: 11px; }
  .sc-head .sc-date { font-size: 10px; }
  .sc-event { padding: 1px 4px; font-size: 10px; left: 1px; right: 1px; border-radius: 3px; }
  .sc-event-meta { font-size: 9px; }

  .schedule-toolbar { gap: 8px; flex-direction: column; align-items: stretch; }
  .schedule-toolbar > * { margin-left: 0 !important; }
  .topbar { gap: 8px; margin-bottom: 14px; }
  .topbar .spacer { display: none; }
  .modal-overlay { padding: 8px; }
  .modal { padding: 18px; max-height: 92vh; border-radius: 10px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions button { width: 100%; }
  .proposal { flex-direction: column; align-items: stretch; gap: 8px; }
  .proposal button { width: 100%; }
  button, .btn { padding: 10px 14px; }
  .btn-sm { padding: 6px 10px; }
  .session-card { flex-wrap: wrap; }
  .list-item { flex-wrap: wrap; }
  input, select, textarea { font-size: 16px; }
}

/* Large desktop / 4K — already capped via .main > * max-width:1600px */
@media (min-width: 1920px) {
  body { font-size: 15px; }
  .main { padding: 36px 48px; }
  .page-title { font-size: 24px; }
}

@media (min-width: 2560px) {
  body { font-size: 16px; }
  .sidebar { width: 300px; }
}

/* Touch-friendly hit area for nav buttons */
@media (hover: none) and (pointer: coarse) {
  .sg-cell { height: 26px; }
  .nav-item { padding: 10px 12px; }
}
