:root {
  color-scheme: light;
  --bg: #f7f7f9;
  --surface: #ffffff;
  --surface-soft: #fafafe;
  --text: #191a1f;
  --muted: #70747f;
  --line: #e4e5ea;
  --accent: #6246df;
  --accent-soft: #f0edff;
  --success: #188248;
  --success-soft: #eaf7ef;
  --danger: #b42318;
  --danger-soft: #fff0ee;
  --sidebar: 244px;
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; }
button, .button { border: 0; }

.app-shell { min-height: 100vh; padding-left: var(--sidebar); }
.sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; width: var(--sidebar); display: flex; flex-direction: column; padding: 28px 18px 22px; background: var(--surface); border-right: 1px solid var(--line); }
.brand { font-size: 22px; font-weight: 820; letter-spacing: -.035em; }
.sidebar-brand { display: block; padding: 0 12px; margin-bottom: 34px; }
.primary-nav, .more-nav nav { display: grid; gap: 5px; }
.primary-nav a, .more-nav nav a { padding: 11px 13px; border-radius: 10px; color: #3d4048; font-weight: 650; }
.primary-nav a:hover, .more-nav nav a:hover { background: #f5f5f8; color: var(--text); }
.primary-nav a.active, .more-nav nav a.active { color: var(--accent); background: var(--accent-soft); }
.more-nav { margin-top: 25px; padding-top: 18px; border-top: 1px solid var(--line); }
.more-nav summary { display: flex; justify-content: space-between; align-items: center; padding: 8px 13px; color: var(--muted); cursor: pointer; list-style: none; font-weight: 650; }
.more-nav summary::-webkit-details-marker { display: none; }
.more-nav summary::after { content: "+"; font-size: 18px; font-weight: 500; }
.more-nav[open] summary::after { content: "−"; }
.more-nav nav { margin-top: 5px; }
.sidebar-logout { margin-top: auto; padding: 18px 13px 0; border-top: 1px solid var(--line); }
.link-button { padding: 0; background: none; color: var(--muted); cursor: pointer; font-weight: 650; }
.link-button:hover { color: var(--text); }
.mobile-topbar, .menu-backdrop { display: none; }
.login-topbar { height: 64px; display: flex; align-items: center; padding: 0 max(24px, calc((100vw - 1160px) / 2)); background: var(--surface); border-bottom: 1px solid var(--line); }

.container { width: min(1180px, calc(100% - 64px)); margin: 0 auto; padding: 48px 0 80px; }
.page-heading, .heading { display: flex; align-items: center; justify-content: space-between; gap: 24px; margin-bottom: 28px; }
h1 { margin: 0; font-size: clamp(28px, 3vw, 38px); line-height: 1.12; letter-spacing: -.04em; }
h2 { margin: 0 0 16px; font-size: 20px; line-height: 1.25; letter-spacing: -.02em; }
p { margin: 6px 0; }
.page-heading p, .heading p, .muted, small, .meta { color: var(--muted); }

.health-status, .status { display: inline-flex; align-items: center; gap: 8px; width: max-content; padding: 7px 11px; border-radius: 999px; color: var(--success); background: var(--success-soft); font-weight: 750; white-space: nowrap; }
.health-status span { width: 8px; height: 8px; border-radius: 50%; background: var(--success); }

.overview-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.overview-card { min-height: 230px; display: flex; flex-direction: column; padding: 25px; border: 1px solid var(--line); border-radius: 15px; background: var(--surface); }
.eyebrow { margin-bottom: 19px; color: var(--muted); font-weight: 650; }
.overview-card > strong { display: block; font-size: 31px; line-height: 1.15; letter-spacing: -.035em; overflow-wrap: anywhere; }
.overview-card > strong.compact-title { font-size: 24px; }
.overview-card p { margin: 9px 0 20px; color: var(--muted); }
.text-action { margin-top: auto; color: var(--accent); font-weight: 750; }
.text-action:hover { text-decoration: underline; text-underline-offset: 3px; }
.dashboard-actions { display: flex; justify-content: flex-end; gap: 10px; margin: 24px 0 30px; }
.button, button { display: inline-flex; align-items: center; justify-content: center; width: max-content; min-height: 42px; padding: 10px 16px; border-radius: 9px; color: #fff; background: var(--accent); cursor: pointer; font-weight: 760; }
.button:hover, button:hover { filter: brightness(.96); }
button.secondary, .button.secondary { color: var(--text); background: #ececf1; }
button.danger { color: var(--danger); background: var(--danger-soft); }

.dashboard-columns { display: grid; grid-template-columns: 1.05fr .95fr; gap: 14px; }
.section-card, .card { padding: 23px; border: 1px solid var(--line); border-radius: 15px; background: var(--surface); }
.section-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 8px; }
.section-header h2 { margin: 0; }
.section-header a { color: var(--accent); font-weight: 700; font-size: 14px; }
.activity-list, .recent-list { display: grid; }
.activity-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; align-items: center; padding: 17px 0; border-bottom: 1px solid var(--line); }
.activity-row:last-child { border-bottom: 0; }
.activity-row:hover strong { color: var(--accent); }
.activity-row span:first-child { min-width: 0; display: grid; gap: 3px; }
.activity-row strong { overflow-wrap: anywhere; }
.activity-row small { color: var(--muted); }
.row-state { padding: 4px 8px; border-radius: 999px; color: var(--accent); background: var(--accent-soft); font-size: 12px; font-weight: 750; }
.row-state.active { color: var(--success); background: var(--success-soft); }
.recent-row { display: grid; grid-template-columns: 9px minmax(0, 1fr) auto; gap: 10px; align-items: center; padding: 14px 0; border-bottom: 1px solid var(--line); }
.recent-row:last-child { border-bottom: 0; }
.recent-row small { text-align: right; font-size: 12px; }
.activity-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); }
.activity-dot.error { background: var(--danger); }
.empty-state, .empty { padding: 26px 0; color: var(--muted); text-align: center; }

.stack { display: flex; flex-direction: column; gap: 15px; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 650; }
input, textarea, select { width: 100%; padding: 10px 12px; border: 1px solid #cfd1d8; border-radius: 9px; background: #fff; color: var(--text); }
/* Keep native selection behavior while normalizing Safari's closed control. */
select {
  min-height: 44px;
  min-width: 0;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.4;
}
select:not([multiple]):not([size]), select:not([multiple])[size="1"] {
  -webkit-appearance: none;
  appearance: none;
  padding: 10px 42px 10px 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20'%3E%3Cpath d='m6 8 4 4 4-4' fill='none' stroke='%2370747f' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 20px;
  cursor: pointer;
}
select:not(:disabled):hover { border-color: var(--accent); }
select:disabled {
  color: var(--muted);
  background-color: var(--surface-soft);
  opacity: 1;
  cursor: not-allowed;
}
@media (forced-colors: active) {
  select:not([multiple]):not([size]), select:not([multiple])[size="1"] {
    -webkit-appearance: auto;
    appearance: auto;
    background-image: none;
    padding-right: 12px;
  }
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus, button:focus-visible, a:focus-visible, summary:focus-visible { outline: 3px solid rgb(98 70 223 / .16); outline-offset: 1px; border-color: var(--accent); }
.narrow { width: min(430px, 100%); margin: 70px auto; }
.alert { padding: 11px 13px; border-radius: 9px; color: var(--danger); background: var(--danger-soft); }
.success-message { padding: 11px 13px; border-radius: 9px; color: var(--success); background: var(--success-soft); }
.inline-form { display: grid; grid-template-columns: 1fr 190px auto; gap: 10px; }
.memory-list { display: grid; gap: 12px; margin-top: 14px; }
.memory-item, .reminder-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 18px; align-items: end; }
.habit-list { display: grid; gap: 15px; margin-top: 15px; }
.habit-card { display: grid; gap: 20px; }
.habit-form { display: grid; grid-template-columns: minmax(220px, 1fr) minmax(360px, 1.4fr) auto; gap: 14px; align-items: end; }
.weekday-picker { display: flex; flex-wrap: wrap; gap: 8px 12px; margin: 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; }
.weekday-picker legend { padding: 0 5px; color: var(--muted); font-size: 13px; font-weight: 650; }
.habit-stats { display: flex; flex-wrap: wrap; gap: 10px; }
.habit-stats span { padding: 7px 10px; border-radius: 9px; background: var(--surface-soft); color: var(--muted); }
.habit-stats strong { color: var(--text); }
.habit-grid { display: grid; grid-template-columns: repeat(7, minmax(72px, 1fr)); gap: 8px; overflow-x: auto; }
.habit-day { min-width: 72px; display: grid; justify-items: center; gap: 4px; padding: 10px 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.habit-day.done { color: var(--success); border-color: #b8dec8; background: var(--success-soft); }
.habit-day.off { color: var(--muted); background: var(--surface-soft); }
.habit-check { width: 34px; min-height: 34px; padding: 0; border-radius: 50%; }
.habit-day.done .habit-check { color: #fff; background: var(--success); }
.habit-check-static { display: grid; place-items: center; width: 34px; height: 34px; }
.habit-archive { margin-top: 15px; }
.habit-archive summary { cursor: pointer; font-weight: 750; }
.reminder-item.inactive, .mode-card.inactive { opacity: .62; }
.reminder-form { display: grid; grid-template-columns: 2fr repeat(4, 1fr) auto; gap: 10px; align-items: end; }
.reminder-form .wide { grid-column: span 2; }
.actions { display: flex; gap: 10px; align-items: center; }
.actions input { max-width: 180px; }
.check { flex-direction: row; align-items: center; font-weight: 500; white-space: nowrap; }
.check input { width: auto; }
.compact-list { display: grid; gap: 9px; }
.compact-list small { float: right; }
.pill { display: inline-block; min-width: 44px; padding: 2px 7px; border-radius: 999px; text-align: center; font-size: 11px; font-weight: 800; }
.pill.ok { color: var(--success); background: var(--success-soft); }
.pill.error { color: var(--danger); background: var(--danger-soft); }
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; white-space: nowrap; }
th { color: var(--muted); font-size: 13px; }
.error-text { max-width: 320px; color: var(--danger); white-space: normal; }
.tool-list { max-width: 260px; white-space: normal; }
button.compact-button { min-height: 34px; padding: 6px 11px; }
.pill.queued, .pill.running, .pill.waiting { color: var(--accent); background: var(--accent-soft); }
.pill.completed { color: var(--success); background: var(--success-soft); }
.pill.failed, .pill.cancelled { color: var(--danger); background: var(--danger-soft); }
code { padding: 2px 5px; border-radius: 5px; background: #eee; }
dl { display: grid; grid-template-columns: 110px 1fr; gap: 8px; }
dt { color: var(--muted); }
dd { margin: 0; overflow-wrap: anywhere; }
.grid-two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.settings-section { display: flex; flex-direction: column; gap: 15px; padding-top: 4px; }
.advanced-settings { border: 1px solid var(--line); border-radius: 12px; background: var(--surface-soft); }
.advanced-settings > summary { display: flex; flex-direction: column; gap: 2px; padding: 16px; cursor: pointer; }
.advanced-settings > summary span { color: var(--muted); font-size: 13px; }
.advanced-settings-body { display: flex; flex-direction: column; gap: 18px; padding: 4px 16px 18px; border-top: 1px solid var(--line); }
.section-heading, .route-rule-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.routing-rules, .mode-list { display: grid; gap: 14px; }
.mode-card { display: grid; gap: 16px; }
.route-rule { display: grid; gap: 12px; padding: 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-soft); }
.route-actions { display: flex; gap: 6px; }
button.icon-button { width: 34px; min-height: 34px; height: 34px; padding: 0; color: var(--text); background: #ececf1; }
button.icon-button.danger { color: var(--danger); background: var(--danger-soft); }

@media (max-width: 980px) {
  .container { width: min(100% - 40px, 900px); }
  .overview-grid { grid-template-columns: 1fr 1fr; }
  .overview-card:last-child { grid-column: auto; min-height: 230px; }
  .dashboard-columns { grid-template-columns: 1fr; }
  .reminder-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .reminder-form .wide { grid-column: span 2; }
}

@media (max-width: 760px) {
  :root { --sidebar: 0px; }
  .mobile-topbar { position: sticky; top: 0; z-index: 25; display: flex; align-items: center; justify-content: space-between; height: 58px; padding: 0 16px; background: rgb(255 255 255 / .96); border-bottom: 1px solid var(--line); }
  .menu-toggle { min-height: 36px; padding: 7px 12px; color: var(--text); background: #ececf1; }
  .sidebar { width: min(290px, 86vw); transform: translateX(-105%); transition: transform .2s ease; box-shadow: 12px 0 30px rgb(20 20 30 / .12); }
  .sidebar.open { transform: translateX(0); }
  .menu-backdrop { position: fixed; inset: 0; z-index: 29; display: block; visibility: hidden; opacity: 0; background: rgb(20 20 30 / .36); transition: opacity .2s ease, visibility .2s ease; }
  .menu-backdrop.open { visibility: visible; opacity: 1; }
  .sidebar-brand { margin-bottom: 24px; }
  .container { width: calc(100% - 32px); padding: 26px 0 60px; }
  .page-heading, .heading { align-items: flex-start; margin-bottom: 22px; }
  .dashboard-heading { flex-direction: column; gap: 14px; }
  h1 { font-size: 30px; }
  .overview-grid { grid-template-columns: 1fr; }
  .overview-card, .overview-card:last-child { grid-column: auto; min-height: 0; padding: 20px; }
  .overview-card > strong { font-size: 26px; }
  .overview-card > strong.compact-title { font-size: 21px; }
  .dashboard-actions { justify-content: stretch; }
  .dashboard-actions .button { flex: 1; width: auto; }
  .section-card, .card { padding: 18px; }
  .section-header { align-items: flex-start; }
  .activity-row { grid-template-columns: 1fr; gap: 9px; }
  .row-state { width: max-content; }
  .recent-row { grid-template-columns: 9px minmax(0, 1fr); }
  .recent-row small { grid-column: 2; text-align: left; }
  .inline-form, .memory-item, .reminder-item, .reminder-form, .habit-form, .grid-two { grid-template-columns: 1fr; }
  .reminder-form .wide { grid-column: auto; }
  .habit-grid { grid-template-columns: repeat(7, minmax(68px, 1fr)); }
  .actions { flex-wrap: wrap; }
  .section-heading { align-items: stretch; flex-direction: column; }
  .section-heading button { width: 100%; }
  table { min-width: 680px; }
}

@media (max-width: 430px) {
  .dashboard-actions { flex-direction: column; }
  .dashboard-actions .button { width: 100%; }
  .page-heading:not(.dashboard-heading) { flex-direction: column; }
}
