/* Base elements and the components every area shares: page column, cards, chips, segmented
   controls, switches, list rows, sheets, bubbles (popovers), menus, alerts and toasts. */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--label);
  font: calc(13px * var(--text-scale))/1.35 var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; margin: 0; text-align: inherit; }
button:disabled { cursor: default; }
a { color: var(--accent-text); text-decoration: none; }
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
img, svg { display: block; }
svg { overflow: visible; }

.icon { width: 1em; height: 1em; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7;
  stroke-linecap: round; stroke-linejoin: round; }
.icon.filled { fill: currentColor; stroke: none; }

.sr-only { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.tabular { font-variant-numeric: tabular-nums; }
.rounded { font-family: var(--font-rounded); }
.secondary { color: var(--label-2); }
.tertiary { color: var(--label-3); }

/* ---------------------------------------------------------------- page column */

.page-col { width: 100%; max-width: var(--col); margin: 0 auto; padding: 0 16px; }
.page-col.narrow { max-width: 640px; }

.card {
  background: var(--row);
  border-radius: var(--radius-card);
  padding: 16px;
}

.section-title { font-size: calc(15px * var(--text-scale)); font-weight: 700; margin: 20px 4px 8px; }
.caption { font-size: calc(11px * var(--text-scale)); font-weight: 600; color: var(--label-2); padding-left: 2px; }
.footnote { font-size: calc(10px * var(--text-scale)); color: var(--label-2); }

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 28px; padding: 4px 12px;
  border-radius: 999px;
  background: var(--chip);
  font-size: calc(13px * var(--text-scale)); font-weight: 600;
  white-space: nowrap;
  transition: background 0.12s, opacity 0.12s, transform 0.12s;
}
.btn:hover:not(:disabled) { background: var(--pressed); }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { opacity: 0.4; }
.btn.accent { background: var(--accent); color: var(--on-accent); }
.btn.accent:hover:not(:disabled) { filter: brightness(1.04); }
.btn.danger { color: var(--red); }
.btn.big { min-height: 40px; padding: 8px 18px; font-size: calc(15px * var(--text-scale)); border-radius: 12px; }
.btn.plain { background: none; padding: 4px 6px; color: var(--accent-text); }
.btn.plain:hover:not(:disabled) { background: var(--hover); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 28px; height: 28px; border-radius: 999px;
  color: var(--label);
  transition: background 0.12s;
}
.icon-btn:hover:not(:disabled) { background: var(--hover); }
.icon-btn:disabled { opacity: 0.35; }
.icon-btn .icon { width: 17px; height: 17px; }

/* ---------------------------------------------------------------- chips and segments */

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 32px; padding: 6px 12px;
  border-radius: 12px;
  background: var(--chip);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  transition: background 0.15s, color 0.15s;
}
.chip:hover:not(.selected):not(:disabled) { background: var(--pressed); }
.chip.selected { background: var(--accent); color: var(--on-accent); }
.chip:disabled { opacity: 0.3; cursor: default; }

/* PanelChoice / the Mac's amber segmented control. */
.segmented {
  position: relative;
  display: flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--chip);
  gap: 0;
}
.segmented button {
  position: relative; z-index: 1;
  flex: 1 1 0; min-width: 0;
  min-height: 24px; padding: 3px 8px;
  border-radius: 999px;
  font-size: calc(13px * var(--text-scale)); font-weight: 600;
  text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
  transition: color 0.2s;
}
.segmented button[aria-pressed="true"] { color: var(--on-accent); }
.segmented button:disabled { opacity: 0.3; cursor: default; }
.segmented .thumb {
  position: absolute; z-index: 0; top: 3px; bottom: 3px; left: 3px;
  border-radius: 999px; background: var(--accent);
  transition: transform 0.25s var(--snappy), width 0.25s var(--snappy), background 0.2s;
}
.segmented.big button { min-height: 36px; font-size: calc(15px * var(--text-scale)); }
.segmented.disabled { opacity: 0.4; pointer-events: none; }

/* The Mac's small switch. */
.switch {
  appearance: none; -webkit-appearance: none;
  position: relative; flex: none;
  width: 32px; height: 19px; margin: 0;
  border-radius: 999px;
  background: var(--gray4);
  cursor: pointer;
  transition: background 0.18s;
}
.switch::after {
  content: ""; position: absolute; top: 1.5px; left: 1.5px;
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 0 0 0.5px rgba(0, 0, 0, 0.06);
  transition: transform 0.18s var(--snappy);
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(13px); }
.switch:disabled { opacity: 0.4; cursor: default; }

/* Sliders: macOS style, amber fill. */
input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 20px; margin: 0;
  background: transparent; cursor: pointer;
  --range-fill: 50%;   /* how much of the track is amber; set by fillRange() (not --fill, the fill colour token) */
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 999px;
  background: linear-gradient(90deg, var(--accent) var(--range-fill), var(--fill) var(--range-fill));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 18px; height: 18px; margin-top: -7px; border-radius: 50%;
  background: #fff; box-shadow: 0 0.5px 2px rgba(0, 0, 0, 0.35), 0 0 0 0.5px rgba(0, 0, 0, 0.08);
}
input[type="range"]::-moz-range-track { height: 4px; border-radius: 999px; background: var(--fill); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 999px; background: var(--accent); }
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px; border: 0; border-radius: 50%;
  background: #fff; box-shadow: 0 0.5px 2px rgba(0, 0, 0, 0.35);
}

/* Text fields. */
.field {
  width: 100%; min-height: 28px; padding: 5px 9px;
  border-radius: 7px;
  border: 0.5px solid var(--hairline);
  background: var(--row);
  font-size: calc(13px * var(--text-scale));
  box-shadow: inset 0 0.5px 1px rgba(0, 0, 0, 0.05);
}
.field:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 0; }
select.field { appearance: none; -webkit-appearance: none; padding-right: 24px;
  background-image: linear-gradient(45deg, transparent 50%, var(--label-2) 50%), linear-gradient(135deg, var(--label-2) 50%, transparent 50%);
  background-position: calc(100% - 12px) 50%, calc(100% - 8px) 50%; background-size: 4px 4px; background-repeat: no-repeat; }

/* ---------------------------------------------------------------- list rows */

.rows { display: flex; flex-direction: column; }
.row {
  position: relative;
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 44px;
  padding: 5px 22px 5px 16px;
  text-align: left;
  border-radius: 10px;
}
button.row:hover, a.row:hover { background: var(--hover); }
button.row:active, a.row:active { background: var(--pressed); }
.row + .row::before {
  content: ""; position: absolute; top: 0; right: 0; left: var(--divider, 76px);
  border-top: 0.5px solid var(--hairline);
}
.row .main { flex: 1; min-width: 0; display: grid; gap: 1px; }
.row .title { font-size: calc(13px * var(--text-scale)); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--label); }
.row .sub { font-size: calc(11px * var(--text-scale)); color: var(--label-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .chev { width: 12px; height: 12px; color: var(--label-3); }
.row.current .title { color: var(--accent-text); }

.form { background: var(--row); border-radius: 10px; padding: 0 12px; }
.form .form-row { display: flex; align-items: center; gap: 12px; min-height: 36px; padding: 6px 0; }
.form .form-row + .form-row { border-top: 0.5px solid var(--hairline); }
.form .form-row > .label { flex: 1; min-width: 0; }
.form-footer { font-size: calc(11px * var(--text-scale)); color: var(--label-2); padding: 6px 12px 0; }
.form-header { font-size: calc(13px * var(--text-scale)); font-weight: 700; padding: 18px 2px 6px; }

/* ---------------------------------------------------------------- glass */

.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(1.8);
  backdrop-filter: blur(20px) saturate(1.8);
  border: 0.5px solid var(--glass-edge);
  box-shadow: var(--glass-shadow);
}

/* ---------------------------------------------------------------- sheets (Mac sheet = centred card) */

dialog.sheet {
  width: min(var(--w, 460px), calc(100vw - 32px));
  max-width: none;
  max-height: min(var(--h, 640px), calc(100dvh - 48px));
  margin: auto;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sheet);
  background: var(--sheet);
  color: var(--label);
  box-shadow: var(--sheet-shadow);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: sheet-in 0.2s var(--snappy); }
dialog.sheet.fixed-h { height: min(var(--h, 640px), calc(100dvh - 48px)); }
dialog.sheet::backdrop { background: var(--scrim); animation: fade-in 0.2s ease; }
dialog.sheet.closing { animation: sheet-out 0.16s ease forwards; }
dialog.sheet.closing::backdrop { animation: fade-out 0.16s ease forwards; }
@keyframes sheet-in { from { opacity: 0; transform: scale(0.97) translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes sheet-out { to { opacity: 0; transform: scale(0.98) translateY(6px); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

.sheet-head {
  display: flex; align-items: center; justify-content: center; position: relative;
  min-height: 48px; padding: 0 56px;
  border-bottom: 0.5px solid var(--hairline);
}
.sheet-head h2 { margin: 0; font-size: calc(13px * var(--text-scale)); font-weight: 700; text-align: center; }
.sheet-head .lead, .sheet-head .trail { position: absolute; top: 50%; transform: translateY(-50%); }
.sheet-head .lead { left: 12px; }
.sheet-head .trail { right: 12px; }
/* Grows from its content, not from 0: Safari 26 sizes a sheet without a fixed height from its
   parts' flex bases, so `flex: 1` (a 0 basis) left the tip jar and the tool sheets 20 px tall. */
.sheet-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 16px; }
.sheet-foot {
  display: flex; justify-content: flex-end; align-items: center; gap: 8px;
  min-height: 56px; padding: 12px 16px;
  border-top: 0.5px solid var(--hairline);
}
.sheet.no-head .sheet-head { display: none; }

/* Below the compact width, a sheet fills the window like the phone's. */
@media (max-width: 639px) {
  dialog.sheet {
    width: 100vw; max-height: none; height: 100dvh;
    border-radius: 0; margin: 0;
  }
  /* A sheet with a fixed height fills the phone's screen too; a card keeps fitting its content. */
  dialog.sheet.fixed-h:not(.keep-card) { height: 100dvh; max-height: none; }
  dialog.sheet.keep-card { width: calc(100vw - 32px); height: fit-content; max-height: calc(100dvh - 48px); margin: auto; border-radius: var(--radius-sheet); }
  dialog.sheet[open] { animation: sheet-up 0.26s var(--snappy); }
  @keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
  .sheet-head { padding-top: env(safe-area-inset-top, 0px); min-height: calc(48px + env(safe-area-inset-top, 0px)); }
  .sheet-head .lead, .sheet-head .trail { top: calc(50% + env(safe-area-inset-top, 0px) / 2); }
}

/* ---------------------------------------------------------------- alerts (Mac alert) */

dialog.alert {
  width: 260px; padding: 20px 16px 16px; border: 0; border-radius: 12px;
  background: var(--sheet); color: var(--label); box-shadow: var(--sheet-shadow);
  text-align: center;
}
dialog.alert[open] { animation: sheet-in 0.18s var(--snappy); }
dialog.alert::backdrop { background: var(--scrim); }
dialog.alert .app-icon { width: 64px; height: 64px; margin: 0 auto 10px; border-radius: 14px; }
dialog.alert h2 { margin: 0 0 6px; font-size: calc(13px * var(--text-scale)); font-weight: 700; }
dialog.alert p { margin: 0 0 14px; font-size: calc(11px * var(--text-scale)); color: var(--label); }
dialog.alert .field { margin-bottom: 14px; text-align: left; }
dialog.alert .alert-buttons { display: grid; gap: 8px; }
dialog.alert .alert-buttons.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
dialog.alert .alert-buttons .btn { min-height: 28px; border-radius: 7px; font-weight: 500; white-space: normal; line-height: 1.2; padding-block: 5px; }
dialog.alert .alert-buttons .btn.accent { font-weight: 600; }

/* ---------------------------------------------------------------- bubbles (popovers) */

.bubble {
  position: fixed; z-index: 60;
  min-width: 120px;
  padding: 10px;
  border-radius: 12px;
  background: var(--menu);
  -webkit-backdrop-filter: blur(30px) saturate(1.6);
  backdrop-filter: blur(30px) saturate(1.6);
  box-shadow: var(--menu-shadow);
  color: var(--label);
  animation: bubble-in 0.15s var(--snappy);
  transform-origin: var(--origin, 50% 100%);
}
.bubble.dark { --label: rgba(255, 255, 255, 0.85); --label-2: rgba(255, 255, 255, 0.55); --chip: rgba(255, 255, 255, 0.08);
  --pressed: rgba(255, 255, 255, 0.14); --accent: #FFB340; --on-accent: #1E1203; --hairline: rgba(255, 255, 255, 0.12);
  --fill: rgba(255, 255, 255, 0.10);
  background: rgba(30, 30, 32, 0.9); box-shadow: 0 0 0 0.5px rgba(255, 255, 255, 0.12), 0 14px 40px rgba(0, 0, 0, 0.5); color-scheme: dark; }
.bubble .arrow {
  position: absolute; width: 13px; height: 13px;
  background: inherit; -webkit-backdrop-filter: inherit; backdrop-filter: inherit;
  transform: rotate(45deg);
  clip-path: polygon(0 0, 100% 0, 0 100%);
}
.bubble.closing { animation: bubble-out 0.12s ease forwards; }
@keyframes bubble-in { from { opacity: 0; transform: scale(0.94); } to { opacity: 1; transform: none; } }
@keyframes bubble-out { to { opacity: 0; transform: scale(0.96); } }
.bubble-title { margin: 0 0 2px; font-size: calc(17px * var(--text-scale)); font-weight: 700; text-align: center; }
.bubble-section { display: grid; gap: 6px; }
.bubble-section > .name { font-size: calc(13px * var(--text-scale)); font-weight: 600; color: var(--label-2); padding-left: 2px; }

/* ---------------------------------------------------------------- menus (pull-down, context) */

.menu {
  position: fixed; z-index: 70;
  min-width: 180px; max-width: 320px;
  padding: 5px;
  border-radius: 10px;
  background: var(--menu);
  -webkit-backdrop-filter: blur(30px) saturate(1.6);
  backdrop-filter: blur(30px) saturate(1.6);
  box-shadow: var(--menu-shadow);
  font-size: calc(13px * var(--text-scale));
  animation: fade-in 0.1s ease;
  overflow-y: auto;
  max-height: calc(100dvh - 16px);
}
.menu .item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; min-height: 24px; padding: 3px 10px 3px 8px;
  border-radius: 6px;
  color: var(--label);
  white-space: nowrap;
}
.menu .item .icon { width: 15px; height: 15px; color: var(--label-2); }
.menu .item .check { width: 12px; flex: none; text-align: center; font-size: calc(12px * var(--text-scale)); }
.menu .item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.menu .item .shortcut { color: var(--label-2); font-size: calc(12px * var(--text-scale)); }
.menu .item:hover:not(:disabled), .menu .item.active:not(:disabled) { background: var(--accent); color: var(--on-accent); }
.menu .item:hover:not(:disabled) .icon, .menu .item.active:not(:disabled) .icon,
.menu .item:hover:not(:disabled) .shortcut { color: inherit; }
.menu .item:disabled { color: var(--label-3); }
.menu .item.destructive { color: var(--red); }
.menu .item.destructive:hover { background: var(--red); color: #fff; }
.menu .sep { height: 0.5px; margin: 5px 8px; background: var(--hairline); }
.menu .heading { padding: 4px 10px 2px; font-size: calc(11px * var(--text-scale)); font-weight: 600; color: var(--label-2); }
.menu.dark { --label: rgba(255, 255, 255, 0.85); --label-2: rgba(255, 255, 255, 0.55); --label-3: rgba(255, 255, 255, 0.25);
  --hairline: rgba(255, 255, 255, 0.12); --accent: #FFB340; background: rgba(40, 40, 42, 0.92); color-scheme: dark; }

/* ---------------------------------------------------------------- toast */

.toast {
  position: fixed; z-index: 90; left: 50%; bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: min(480px, calc(100vw - 32px));
  padding: 9px 16px;
  border-radius: 999px;
  background: rgba(30, 30, 32, 0.92); color: #fff;
  font-size: calc(13px * var(--text-scale)); font-weight: 600; text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
  animation: toast-in 0.2s var(--snappy);
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------------------------------------------------------------- marquee text */

.marquee { position: relative; overflow: hidden; white-space: nowrap; }
.marquee .measure { visibility: hidden; display: inline-block; }
.marquee .run { position: absolute; left: 0; top: 0; white-space: nowrap; will-change: transform; }
.marquee.overflowing { -webkit-mask-image: linear-gradient(90deg, #000 0, #000 90%, transparent 100%); mask-image: linear-gradient(90deg, #000 0, #000 90%, transparent 100%); }

/* ---------------------------------------------------------------- touch screens
   A finger needs about 44 points (Apple's guideline); the Mac's controls are smaller. On a touch
   screen the small buttons keep their look and take the tap in an invisible 44-point square
   around them (::after, centred, only where the button is smaller); the switches, segments, menus
   and pop-ups a finger mostly uses grow to iOS's sizes. Buttons in a tight row (Library's star and
   ⋯, Stage Mode's tools) are left as they are, so one never takes the other's tap. */

@media (pointer: coarse) {
  :where(.btn, .icon-btn, .titlebar .back, .title-install, .lib-corner-btn, .lib-search-clear,
    .lib-text-btn, .journal-corner, .home-week-link, .settings-copy, .settings-popup, .settings-row-link,
    .nudge-close, .stage-close, .stage-title, .stage-dot, .tools-preset) { position: relative; }
  :where(.btn, .icon-btn, .titlebar .back, .title-install, .lib-corner-btn, .lib-search-clear,
    .lib-text-btn, .journal-corner, .home-week-link, .settings-copy, .settings-popup, .settings-row-link,
    .nudge-close, .stage-close, .stage-title, .tools-preset)::after {
    content: ""; position: absolute;
    top: min(0px, calc((100% - 44px) / 2)); bottom: min(0px, calc((100% - 44px) / 2));
    left: min(0px, calc((100% - 44px) / 2)); right: min(0px, calc((100% - 44px) / 2));
  }
  /* Stage Mode's page dots sit 7 px apart: taller, and as wide as the gap allows. */
  :where(.stage-dot)::after { content: ""; position: absolute; top: -15px; bottom: -15px; left: -3.5px; right: -3.5px; }

  /* iOS's switch, 51 × 31. (:root outranks the areas' own sheets, which load after this one.) */
  :root .switch, :root .tools-switch-look { width: 51px; height: 31px; }
  :root .switch::after, :root .tools-switch-look::after { top: 2px; left: 2px; width: 27px; height: 27px; }
  :root .switch:checked::after, :root .tools-switch-look.on::after { transform: translateX(20px); }

  :root .segmented button { min-height: 28px; }
  :root .menu .item { min-height: 40px; }
  :root .journal-popup, :root .tools-select, :root .settings-popup { min-height: 32px; }
  :root .settings-row { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
