/* Practiae front-only. CSS variables only — works in dark and light mode. */
:root {
  --bg: #f7f5f2; --surface: #ffffff; --surface2: #efeae6;
  --text: #1c1a17; --muted: #6b6560; --border: #ddd6cf;
  --accent: #5a178a; --accent2: #7a4212; --danger: #b3261e;
  --radius: 8px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #171512; --surface: #221f1c; --surface2: #2e2a26;
    --text: #f0ebe6; --muted: #a89f96; --border: #3d3833; --accent: #c9a0e8; --accent2: #e0a878; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
  font-family: "DM Sans", system-ui, sans-serif; line-height: 1.45; }
h1 { margin: 0; font-family: Georgia, serif; font-size: 1.6rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 .5rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.subtitle { margin: 0; color: var(--muted); font-size: .9rem; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  padding: .75rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.topbar-brand { display: flex; align-items: center; gap: .6rem; }
.topbar-brand img { border-radius: 6px; flex-shrink: 0; }
.brand-icon { vertical-align: -3px; }
.top-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
/* Maximise usable space: no narrow centred column, small side padding. */
.layout { display: grid; grid-template-columns: 240px 1fr; gap: 1.5rem; padding: 1rem 1.25rem; }
@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; }
  .program-settings { flex-direction: column; align-items: stretch; }
  .program-settings label { display: flex; flex-direction: column; gap: .25rem; }
  .program-settings input { width: 100%; }
}
.programs { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; height: fit-content; }
.programs ul { list-style: none; padding: 0; margin: 0 0 1rem; }
.program-item { display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 6px;
  padding: .5rem; cursor: pointer; color: var(--text); }
.program-item:hover { background: var(--surface2); }
.program-item.active { background: var(--accent); color: #fff; }
.program-item.active .program-meta { color: #e8d5f5; }
.program-name { display: block; font-weight: 600; }
.program-meta { font-size: .8rem; color: var(--muted); }
.program-actions { display: flex; gap: .4rem; margin-top: .75rem; flex-wrap: wrap; }
.btn { background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: .45rem .8rem; cursor: pointer; font: inherit; }
.btn:hover { background: var(--surface2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.small { padding: .25rem .5rem; font-size: .85rem; }
.btn.active { outline: 2px solid var(--accent); }
.btn:disabled { opacity: .5; cursor: wait; }
.badge { background: var(--surface2); border: 1px solid var(--border); border-radius: 999px; padding: .2rem .6rem; font-size: .85rem; }
.badge[data-state="error"] { color: var(--danger); border-color: var(--danger); }
.badge[data-state="saving"] { opacity: .7; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin: .75rem 0; }
.row .grow { flex: 1; }
label { font-size: .9rem; }
input[type="text"], input[type="number"], input[type="search"], textarea { background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: .4rem .5rem; font: inherit; }
input[type="text"] { width: 100%; }
textarea { width: 100%; }
.pieces-table, .routine-table { width: 100%; border-collapse: collapse; background: var(--surface); }
.pieces-table th, .pieces-table td, .routine-table th, .routine-table td {
  border-bottom: 1px solid var(--border); padding: .45rem .5rem; text-align: left; vertical-align: middle; }
.pieces-table th { font-size: .85rem; color: var(--muted); }
.pieces-table input[type="range"] { width: 110px; vertical-align: middle; }
/* Slider + its % stay on one line (table cell and mobile card alike). */
.perc-wrap { display: inline-flex; align-items: center; gap: .4rem; white-space: nowrap; }
.perc-val { display: inline-block; min-width: 3ch; }
.gap-wrap { display: inline-flex; align-items: center; gap: .5rem; white-space: nowrap; }
.gap-wrap input[type="number"] { width: 4.5em; }
.gap-val { color: var(--muted); font-size: .85rem; }
/* The UA [hidden] rule loses to author display rules (e.g. the
   .program-settings label flex rule), so enforce it explicitly. */
[hidden] { display: none !important; }
.tag { display: inline-block; border: 1px solid var(--tag-color, var(--accent)); color: var(--tag-color, var(--accent));
  border-radius: 999px; padding: 0 .45rem; font-size: .8rem; margin: 1px 2px 1px 0; }
.editable { cursor: text; }
.empty { color: var(--muted); text-align: center; }
.inline-edit { width: 100%; }
.duration-edit { display: inline-flex; align-items: center; gap: 4px; }
.duration-edit .inline-edit { width: 3.5em; }
.filter-tags { display: flex; gap: .5rem; flex-wrap: wrap; }
.filter-tag { font-size: .85rem; }
/* Tag distribution chart — vertical stacked bars, ported from the Flask
   app: one column per day, total on top, tooltip on segment hover. */
.tag-dist-chart { display: flex; align-items: flex-end; gap: 6px; min-height: 180px; padding-top: 8px; }
.tag-dist-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 0; }
.tag-dist-bar-wrapper { width: 100%; display: flex; flex-direction: column; justify-content: flex-end; position: relative; }
.tag-dist-segment { width: 100%; min-height: 0; border-radius: 2px; position: relative; }
.tag-dist-segment:first-child { border-radius: 4px 4px 2px 2px; }
.tag-dist-segment:last-child { border-radius: 2px 2px 4px 4px; }
.tag-dist-segment:only-child { border-radius: 4px; }
.tag-dist-col-label { font-size: .75rem; color: var(--muted); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tag-dist-col-total { font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.tag-dist-segment-tooltip { display: none; position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px;
  font-size: .75rem; white-space: nowrap; z-index: 10; pointer-events: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .12); color: var(--text); }
.tag-dist-segment:hover .tag-dist-segment-tooltip { display: block; }
.chart-mode { display: flex; gap: .5rem; align-items: center; font-size: .85rem; color: var(--muted); margin: .5rem 0; }
.chart-mode input { accent-color: var(--accent); }
/* Routine cell highlighting — ported from the Flask app: hovering a cell
   highlights the same piece everywhere (blue), pieces it must not share
   a day with (warm) and pieces it must share days with (green). */
.routine-table td[data-piece] { transition: background .15s ease; cursor: pointer; }
.routine-table td.piece-highlight { background: color-mix(in srgb, #4a6fd6 18%, transparent); }
.routine-table td.exclusion-highlight { background: color-mix(in srgb, #c65a2e 18%, transparent); }
.routine-table td.inclusion-highlight { background: color-mix(in srgb, #2e9e5b 18%, transparent); }
@media (prefers-color-scheme: dark) {
  .routine-table td.piece-highlight { background: color-mix(in srgb, #4a6fd6 28%, transparent); }
  .routine-table td.exclusion-highlight { background: color-mix(in srgb, #c65a2e 28%, transparent); }
  .routine-table td.inclusion-highlight { background: color-mix(in srgb, #2e9e5b 28%, transparent); }
}
/* Wide tables scroll horizontally instead of squeezing on narrow screens. */
.table-scroll { overflow-x: auto; max-width: 100%; }
.table-scroll table { min-width: 720px; }
/* Grid children default to min-width:auto, which lets a wide table stretch
   the whole column (and the page) instead of scrolling inside its wrapper. */
.layout > * { min-width: 0; }
@media (prefers-color-scheme: dark) {
  /* Tag colours are ported from Flask; "Chap. I." is near-black and would
     vanish on a dark surface, so tags and chart segments are lightened. */
  .tag { color: color-mix(in srgb, var(--tag-color) 55%, white);
    border-color: color-mix(in srgb, var(--tag-color) 55%, white); }
  .tag-dist-segment, .legend-item i { filter: brightness(1.7); }
}
.legend { display: flex; gap: .75rem; flex-wrap: wrap; font-size: .85rem; margin-top: .5rem; }
.legend-item i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: .3rem; }
.days-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .4rem; margin: 1rem 0; }
.tags-chips { display: flex; flex-wrap: wrap; gap: .35rem; margin: .75rem 0; }
#tags-search { width: 100%; }
.tag-remove { background: none; border: 0; color: inherit; cursor: pointer; padding: 0 .1rem; font-size: 1rem; line-height: 1; }
.tags-options { display: flex; flex-direction: column; gap: 2px; margin: .75rem 0; max-height: 40vh; overflow-y: auto; }
.tag-option { display: flex; align-items: center; gap: .5rem; background: none; border: 0; padding: .4rem .25rem; cursor: pointer; text-align: left; font: inherit; color: var(--text); border-radius: 6px; }
.tag-option:hover { background: var(--surface2); }
.tag-option .tag-tick { width: 1.2em; text-align: center; color: var(--accent); flex: none; }
.tag-option.tag-create { color: var(--accent); }
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; max-width: 520px; width: calc(100% - 2rem); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.draft-piece { display: block; margin: .25rem 0; }
/* Demo mode: Drive controls are hidden — same UI otherwise. */
body.demo .drive-only { display: none; }

/* ---- Welcome page ----
   DeLaClaw principles: DM Sans, Lucide icons only (no emoji), card
   headers solid in the card colour with white text/icons, 6% tinted
   card body via color-mix, stat cards with a coloured icon + big value
   + muted label (DeLaClaw welcome-stats pattern). */
.welcome-page { max-width: 1180px; margin: 0 auto; padding: 2rem 1.25rem 3rem; }
.welcome-hero { text-align: center; margin-bottom: 2rem; }
.welcome-logo-img { margin-bottom: .5rem; }
.welcome-hero h1 { font-size: 2.4rem; }
.welcome-tagline { font-size: 1.15rem; font-weight: 600; margin: .75rem 0 .5rem; }
.welcome-hero .muted { max-width: 560px; margin: 0 auto; }
.welcome-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 2rem; }
.choice-card { display: block; text-decoration: none; color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  background: var(--surface); }
.choice-card:hover { box-shadow: 0 4px 16px rgba(0, 0, 0, .12); }
.choice-header { display: flex; align-items: center; gap: .6rem;
  background: var(--card-color); color: #fff; padding: .9rem 1.1rem; }
.choice-header h2 { margin: 0; font-size: 1.15rem; color: #fff; }
.choice-header .lucide-icon, .choice-header img { flex-shrink: 0; }
.choice-body { padding: 1rem 1.1rem;
  background: color-mix(in srgb, var(--card-color) 6%, var(--surface)); }
.choice-body p { margin: 0 0 .75rem; }
.choice-body code { background: var(--surface2); border-radius: 4px; padding: 0 .3rem; }
.choice-cta { font-weight: 700; color: var(--accent); display: inline-flex; align-items: center; gap: .3rem; }
.welcome-note { text-align: center; margin: 1rem 0 0; }
.welcome-preview { margin-top: 2rem; }
.welcome-stats-grid { display: flex; gap: 12px; flex-wrap: wrap; }
.welcome-stat-card { flex: 1; min-width: 140px; padding: 12px 14px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border); }
.welcome-stat-top { display: flex; gap: .5rem; align-items: center; }
.welcome-stat-icon { display: inline-flex; }
.welcome-stat-value { font-size: 1.4rem; font-weight: 700; line-height: 1.2; }
.welcome-stat-label { font-size: .82rem; color: var(--muted); margin-top: 2px; }
.welcome-steps { margin-top: 2.5rem; }
.welcome-steps ol { list-style: none; padding: 0; margin: 0;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.welcome-steps li { background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem; display: flex; flex-direction: column; gap: .35rem; }
.welcome-steps .step-icon { color: var(--accent); display: inline-flex; }
.welcome-footer { text-align: center; margin-top: 2.5rem; }
@media (max-width: 800px) {
  .welcome-page { padding: 1.25rem 1rem 3rem; }
  .welcome-hero h1 { font-size: 1.9rem; }
  .welcome-choices { grid-template-columns: 1fr; }
  .welcome-steps ol { grid-template-columns: 1fr; }
}

/* ---- Mobile rethink (≤800px): cards, not tables ----
   Desktop keeps the dense 9-column table and 8-column routine grid.
   On a phone those tables are unusable, so mobile swaps in:
   - piece cards (big title, full-width slider, tap-to-edit, thumb actions)
   - a day-by-day routine view (chips + prev/next, one day's list)
   - a sticky Generate bar so the action is always in reach
   - programs as a horizontal chip row, filters as tap-sized pills.
   The card list and the table render from the same data and share the
   same data-actions, so one delegated handler serves both. */
.pieces-cards, .routine-mobile, .mobile-bar { display: none; }
.view-toggle { display: none; } /* phone-only: Pieces / Routine switch */
#search-pieces { flex: 1 1 180px; min-width: 140px; }
@media (max-width: 800px) {
  body { padding-bottom: 4.5rem; }
  .topbar { padding: .75rem 1rem; }
  h1 { font-size: 1.3rem; }
  .layout { padding: 1rem; gap: 1rem; }
  /* 44px minimum touch targets. */
  .btn { min-height: 44px; }
  .btn.small { min-height: 44px; padding: .5rem .75rem; }
  /* View toggle: Pieces (edit) vs Routine (day view). Sticky so it stays
     in reach while scrolling; the two views swap via a body class. */
  .view-toggle { display: flex; position: sticky; top: 0; z-index: 10; background: var(--bg); padding: .5rem 0; }
  .view-toggle-btn { flex: 1; min-height: 44px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; }
  .view-toggle-btn:first-child { border-radius: 999px 0 0 999px; }
  .view-toggle-btn:last-child { border-radius: 0 999px 999px 0; }
  .view-toggle-btn + .view-toggle-btn { border-left: 0; }
  .view-toggle-btn[aria-selected="true"] { background: var(--accent); color: #fff; border-color: var(--accent); }
  #routine-view { display: none; }
  body.mobile-view-routine #routine-view { display: block; }
  body.mobile-view-routine #pieces-view { display: none; }
  /* Programs: horizontal chip row instead of a tall list. */
  .programs ul { display: flex; gap: .5rem; overflow-x: auto; }
  .program-item { width: auto; white-space: nowrap; flex-shrink: 0; }
  .program-item .program-meta { display: none; }
  /* Filters: search first and full-width, tags as pills. */
  #search-pieces { order: -1; flex-basis: 100%; min-height: 44px; }
  .filter-tag { padding: .45rem .7rem; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); }
  .filter-tag input { accent-color: var(--accent); }
  /* Pieces: cards in, table out. */
  .table-scroll:has(.pieces-table) { display: none; }
  .pieces-cards { display: flex; flex-direction: column; gap: .75rem; }
  .piece-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .85rem 1rem; }
  .card-head { display: flex; justify-content: space-between; gap: .5rem; align-items: flex-start; }
  .card-title { font-weight: 700; font-size: 1.05rem; }
  .card-composer { color: var(--muted); }
  .card-rank { color: var(--muted); font-size: .85rem; flex-shrink: 0; }
  .card-freq { display: flex; align-items: center; gap: .5rem; margin: .6rem 0 .4rem; }
  .card-freq .perc-wrap { flex: 1; display: flex; align-items: center; gap: .5rem; min-width: 0; }
  .card-freq input[type="range"] { flex: 1; min-width: 0; height: 28px; accent-color: var(--accent); }
  .perc-val { font-weight: 600; min-width: 3.5ch; }
  .card-freq-hint { color: var(--muted); font-size: .85rem; }
  .card-tags { display: flex; flex-wrap: wrap; gap: .3rem; margin: .25rem 0 .5rem; }
  .card-meta { margin: .25rem 0; }
  .card-actions { display: flex; gap: .5rem; margin-top: .6rem; }
  .card-actions .btn { flex: 1; min-width: 0; }
  /* Routine: one day at a time, table out. */
  #routine-box { display: none; }
  .routine-mobile { display: block; }
  .day-nav { display: flex; gap: .5rem; align-items: flex-start; margin: .5rem 0; }
  .day-nav > .btn { flex-shrink: 0; min-width: 44px; }
  .day-chips { display: flex; gap: .35rem; overflow-x: auto; flex: 1; padding-bottom: .25rem; }
  .day-chip { flex-shrink: 0; min-height: 44px; padding: .4rem .7rem; border: 1px solid var(--border);
    border-radius: 999px; background: var(--surface); color: var(--text); }
  .day-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
  .day-list { list-style: none; padding: 0; margin: .5rem 0; }
  .day-list li { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem .85rem; margin-bottom: .5rem; cursor: pointer; }
  .day-list li .day-tick { display: inline-block; width: 1.4em; color: var(--accent); font-weight: 700; }
  .day-list li.done .day-label { color: var(--muted); text-decoration: line-through; }
  /* The Generate button lives in the sticky bar on mobile. */
  .program .row [data-action="generate"] { display: none; }
  /* Sticky bottom bar: Generate is always in reach, with the live count. */
  .mobile-bar { display: flex; position: sticky; bottom: 0; z-index: 10; background: var(--surface);
    border-top: 1px solid var(--border); padding: .6rem 1rem; gap: .75rem;
    align-items: center; justify-content: space-between; box-shadow: 0 -4px 12px rgba(0, 0, 0, .08); }
  .mobile-bar .btn.primary { flex-shrink: 0; }
}
