:root {
  --bg: #f2f2f7; --card: #ffffff; --text: #1c1c1e; --muted: #6c6c70; --line: #e3e3e8;
  --accent: #5b5bd6; --accent-text: #ffffff; --danger: #d6343b; --warn: #b9770e; --ok: #1f9d6a;
  --radius: 16px; --gutter: 16px; --rail: 76px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0e0e13; --card: #1b1b23; --text: #f2f2f7; --muted: #a0a0aa; --line: #2c2c36; --accent: #8b8bff; --accent-text: #0e0e13; --danger: #ff6b72; --warn: #f5b049; --ok: #4cd6a0; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Shell: phone = single pane + bottom bar ---------- */
#app { min-height: 100dvh; display: grid; grid-template-columns: 1fr; padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
#left { display: flex; flex-direction: column; min-width: 0; }
#right, #hinge { display: none; }
main { flex: 1; padding: var(--gutter); padding-bottom: calc(150px + env(safe-area-inset-bottom)); max-width: 960px; width: 100%; margin: 0 auto; }
nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; display: flex; justify-content: space-around; background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(16px); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
nav button { background: none; border: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 56px; min-height: 48px; padding: 4px 8px; color: var(--muted); font-size: 11px; border-radius: 12px; }
nav button[aria-current="page"] { color: var(--accent); font-weight: 600; }
nav .ico { font-size: 20px; line-height: 1; }

/* ---------- Wide screens, tablets, and unfolded foldables: two panes + side rail ---------- */
@media (min-width: 900px), (horizontal-viewport-segments: 2), (vertical-viewport-segments: 2) {
  #app { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); height: 100dvh; overflow: hidden; }
  #left { flex-direction: row; overflow: hidden; min-height: 0; }
  #left > main { overflow-y: auto; padding-bottom: 96px; max-width: none; margin: 0; }
  #right { display: block; overflow-y: auto; padding: var(--gutter); padding-bottom: 96px; border-left: 1px solid var(--line); }
  nav { position: static; flex-direction: column; justify-content: flex-start; gap: 4px; width: var(--rail); padding: 12px 6px; border-top: 0; border-right: 1px solid var(--line); backdrop-filter: none; background: var(--card); }
}

/* ---------- Dual-screen / foldable (spanned across the hinge) ----------
   Left screen: rail + primary view. Right screen: companion pane. Nothing is drawn under the hinge. */
@media (horizontal-viewport-segments: 2) {
  #app { grid-template-columns: env(viewport-segment-width 0 0) calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0)) minmax(0, 1fr); }
  #hinge { display: block; }
  #right { border-left: 0; }
}
@media (vertical-viewport-segments: 2) {
  #app { grid-template-columns: 1fr; grid-template-rows: env(viewport-segment-height 0 0) calc(env(viewport-segment-top 0 1) - env(viewport-segment-bottom 0 0)) minmax(0, 1fr); }
  #hinge { display: block; }
  #left { flex-direction: row; }
  #right { border-left: 0; border-top: 1px solid var(--line); }
}
/* Unfolded landscape on a single large display: roomier gutters and denser lists */
@media (min-width: 1100px) and (orientation: landscape) { :root { --gutter: 24px; } }

/* ---------- Components ---------- */
h1 { font-size: 1.7rem; margin: 4px 0 12px; letter-spacing: -0.01em; }
h2 { font-size: 1rem; margin: 0 0 8px; display: flex; gap: 8px; align-items: center; }
.sub { color: var(--muted); font-size: .85rem; }
.card { background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.row { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; border-top: 1px solid var(--line); background: none; border-left: 0; border-right: 0; border-bottom: 0; width: 100%; text-align: left; }
.row:first-of-type { border-top: 0; }
.bar { width: 4px; align-self: stretch; border-radius: 4px; flex: none; min-height: 34px; }
.grow { flex: 1; min-width: 0; }
.title { font-weight: 600; overflow-wrap: anywhere; }
.badge { font-size: .75rem; color: var(--muted); margin-left: 6px; }
.conflict { color: var(--danger); }
.check { width: 32px; height: 32px; border-radius: 50%; border: 2px solid currentColor; background: none; flex: none; display: grid; place-items: center; }
.check[aria-pressed="true"] { background: var(--ok); border-color: var(--ok); color: #fff; }
.done { text-decoration: line-through; color: var(--muted); }
.seg { display: flex; background: color-mix(in srgb, var(--muted) 18%, transparent); border-radius: 10px; padding: 2px; margin: 0 0 12px; overflow-x: auto; }
.seg button { flex: 1; border: 0; background: none; padding: 7px 10px; border-radius: 8px; white-space: nowrap; min-height: 36px; }
.seg button[aria-pressed="true"] { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.bar-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 8px 12px; min-height: 40px; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.banner { background: color-mix(in srgb, var(--accent) 14%, transparent); border-radius: 12px; padding: 10px 12px; font-size: .85rem; margin-bottom: 12px; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.fab { position: fixed; right: calc(16px + env(safe-area-inset-right)); bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 6; width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); font-size: 28px; box-shadow: 0 6px 16px rgba(0,0,0,.28); }
@media (min-width: 900px), (horizontal-viewport-segments: 2), (vertical-viewport-segments: 2) { .fab { bottom: calc(20px + env(safe-area-inset-bottom)); } }
.grid7 { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 3px; }
.dayhead { text-align: center; font-size: .75rem; color: var(--muted); }
.cell { border: 0; background: none; border-radius: 10px; min-height: 52px; padding: 4px 0; display: flex; flex-direction: column; align-items: center; gap: 3px; }
.cell .n { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; }
.cell[aria-pressed="true"] .n { background: var(--accent); color: var(--accent-text); }
.cell.out { opacity: .45; }
.dots { display: flex; gap: 2px; height: 6px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; }
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.week .col { min-width: 0; }
.chip { font-size: .72rem; border-radius: 6px; padding: 3px 4px; margin-top: 3px; overflow-wrap: anywhere; border-left: 3px solid; background: color-mix(in srgb, var(--muted) 12%, transparent); width: 100%; text-align: left; border-top: 0; border-right: 0; border-bottom: 0; }
@media (max-width: 520px) { .week { display: block; } .week .col { margin-bottom: 8px; } }

/* ---------- Dialog / forms ---------- */
dialog { border: 0; border-radius: 20px; padding: 0; width: min(560px, 100vw - 24px); max-height: 92dvh; background: var(--card); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form, dialog .pad { padding: 18px; display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-size: .85rem; color: var(--muted); }
label.inline { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 1rem; }
input[type=text], input[type=password], input[type=date], input[type=datetime-local], input[type=number], select, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); min-height: 44px; }
input[inputmode=numeric].pin { text-align: center; font-size: 1.6rem; letter-spacing: .5em; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.error { color: var(--danger); font-size: .9rem; }
.note { font-size: .8rem; color: var(--muted); }
.lock { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.lock .card { width: min(380px, 100%); display: grid; gap: 12px; text-align: center; }
@media (prefers-reduced-motion: no-preference) { .btn, nav button, .cell { transition: background-color .15s; } }
@media (forced-colors: active) { .bar { forced-color-adjust: none; } }
