/* ============================================================
   Prayer Counter — global stylesheet
   Dark default; light via [data-theme="light"].
   Mobile-first; all colors via CSS custom properties.
   ============================================================ */

:root {
  color-scheme: dark;
  --bg: #14151a;
  --surface: #1d1f27;
  --surface2: #262a35;
  --border: #33374a;
  --fg: #e9eaf2;
  --fg-dim: #9aa0b6;
  --accent: #7aa2f7;
  --accent-strong: #5b8bf0;
  --accent-soft: rgba(122, 162, 247, 0.14);
  --success: #7bd88f;
  --danger: #f7768e;
  --danger-soft: rgba(247, 118, 142, 0.14);
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 10px 32px rgba(0, 0, 0, 0.38);
  --nav-h: 62px;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f4f9;
  --surface: #ffffff;
  --surface2: #eceef5;
  --border: #d9dce8;
  --fg: #1c1e2a;
  --fg-dim: #5b6174;
  --accent: #4c66d6;
  --accent-strong: #3b53c0;
  --accent-soft: rgba(76, 102, 214, 0.12);
  --success: #2f9e55;
  --danger: #d43d5c;
  --danger-soft: rgba(212, 61, 92, 0.12);
  --shadow: 0 10px 26px rgba(28, 30, 48, 0.14);
}

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

html, body { height: 100%; overflow: hidden; } /* no page scroll — screens scroll internally */

body {
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 620px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }
input, textarea, select { font: inherit; color: inherit; }

/* ---------- layout ---------- */

.screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
.screen.no-nav { padding-bottom: 0; }

.screen-head {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.screen-head h1 { font-size: 18px; font-weight: 700; flex: 1; min-width: 0; }
.screen-head .actions { display: flex; gap: 6px; align-items: center; }

.screen-body { flex: 1; padding: 14px 16px 24px; }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--fg);
  font-weight: 600;
  transition: transform 0.06s ease, background 0.15s ease;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: var(--accent-strong); }
.btn-danger {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
}
.btn-danger:active { background: color-mix(in srgb, var(--danger) 20%, var(--danger-soft)); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 10px; font-size: 13px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--fg-dim);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  touch-action: manipulation;
}
.icon-btn:active { background: var(--border); }
.icon-btn.danger { color: var(--danger); }

/* ---------- bottom nav ---------- */

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0 auto;
  max-width: 620px;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 10;
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--fg-dim);
  font-size: 11px;
  font-weight: 600;
}
.nav-btn .glyph { font-size: 19px; line-height: 1; }
.nav-btn.active { color: var(--accent); }

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

.list { display: flex; flex-direction: column; gap: 10px; }

.row {
  display: flex;
  gap: 10px;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
}

.row.editing { align-items: flex-start; }
.row.editing .row-main { padding-top: 2px; }
.row.editing .row-move { padding-top: 8px; justify-content: flex-start; }
.row.editing .row-actions { padding-top: 8px; justify-content: flex-start; }

.row-move { display: flex; flex-direction: column; gap: 4px; justify-content: center; }
.row-move .icon-btn { width: 30px; height: 26px; font-size: 11px; background: var(--surface2); }

.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; justify-content: center; }
.row-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-preview {
  color: var(--fg-dim);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--fg-dim); flex-wrap: wrap; }
.row-meta .badge { color: var(--accent); font-weight: 600; }
.row-actions { display: flex; flex-direction: column; justify-content: center; gap: 4px; }
.row-actions .icon-btn { width: 34px; height: 30px; }
/* Sessions rows: actions on a single horizontal line, title can wrap 2 lines. */
.row-actions.inline { flex-direction: row; align-items: center; gap: 6px; }
.row-actions.inline .icon-btn { width: 34px; height: 28px; }
.row-actions.inline .btn { flex: 1; min-width: 96px; white-space: nowrap; padding: 8px 10px; }
.session-row .row-title { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.35; }

.empty {
  text-align: center;
  color: var(--fg-dim);
  padding: 48px 20px;
  font-size: 14px;
}

/* ---------- forms ---------- */

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field-label { font-size: 12px; font-weight: 700; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.04em; }
.text-input, .text-area, .select {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 12px;
  outline: none;
}
.text-input:focus, .text-area:focus, .select:focus { border-color: var(--accent); }
.text-area { resize: vertical; min-height: 96px; }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.toggle-label { flex: 1; }
.toggle-input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-slider {
  position: relative;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: var(--border);
  transition: background 0.18s ease;
  flex-shrink: 0;
}
.toggle-slider::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.18s ease;
}
.toggle-input:checked + .toggle-slider { background: var(--accent); }
.toggle-input:checked + .toggle-slider::after { transform: translateX(20px); }
/* Disabled settings rows (e.g. auto-advance with start-count 'one') fade out. */
.settings-row.disabled { opacity: 0.55; }
.settings-row.disabled .toggle-input:disabled + .toggle-slider { opacity: 0.6; }

/* ---------- settings ---------- */

.settings-section { margin-bottom: 20px; }
.settings-title { font-size: 12px; font-weight: 800; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 8px; }
.settings-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 10px; }
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row .label { font-weight: 600; font-size: 14px; }
.settings-row .sub { font-size: 12px; color: var(--fg-dim); }
.settings-row .control {
  min-width: 150px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
}
.settings-row .control .select { width: 100%; min-width: 140px; }

/* ---------- editor ---------- */

.editor {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px;
  margin-top: 6px;
}
.editor-actions { display: flex; gap: 8px; justify-content: flex-start; }
.editor-actions .editor-spacer { flex: 1; }
.editor-actions.add-bottom { margin-top: 14px; justify-content: center; }
.editor-delete { margin-left: 2px; }

/* Session composer steps: full-width blocks, always-open editors. */
.step-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 8px;
}
.step-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.step-head-label {
  flex: 1;
  min-width: 0;
  font-weight: 700;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.step-actions { display: flex; gap: 6px; }
.step-children {
  margin: 8px 0 0 12px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- saved sessions ---------- */

.saved-title { font-size: 12px; font-weight: 800; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.06em; margin: 18px 0 8px; }
.saved-card {
  background: linear-gradient(135deg, var(--accent-soft), transparent 70%), var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
}
.saved-card .row-title { font-size: 15px; }
.saved-card .row-preview { margin: 2px 0 8px; }
.saved-actions { display: flex; gap: 8px; }

/* ---------- counter screen ---------- */

.counter-screen {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.counter-top {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: nowrap;
}
.counter-top .icon-btn { font-size: 17px; }
.counter-top .icon-btn.btn-exit {
  color: var(--fg);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  width: auto;
  height: 38px;
  font-size: 16px;
}
.counter-top .title { font-weight: 700; font-size: 15px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.counter-tools { display: flex; gap: 6px; align-items: center; margin-left: auto; flex-shrink: 0; }

.counter-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.counter-head { text-align: center; margin-bottom: 6px; }
.counter-group { color: var(--accent); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; }
.counter-title { font-size: 21px; font-weight: 800; margin-top: 2px; }
.counter-text { color: var(--fg-dim); font-size: 14px; margin: 8px auto 0; max-width: 34em; white-space: pre-wrap; }
.counter-prayer-text {
  color: var(--fg);
  font-size: 15px;
  overflow-y: auto;
  padding: 10px 12px;
  white-space: pre-wrap;
}
.pane-wrap.text-pane-wrap { display: flex; flex-direction: column; margin-top: 10px; }
.pane.text-pane {
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.pane .counter-prayer-text { max-height: none; margin: 0; }
.drag-handle {
  flex-shrink: 0;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: row-resize;
  color: var(--fg-dim);
  font-size: 10px;
  letter-spacing: 3px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.drag-handle::before { content: "⋮⋮"; }

.counter-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 200px;
  touch-action: manipulation;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.counter-stage-spacer { cursor: default; }
.counter {
  position: relative;
  width: min(300px, 62vw);
  height: min(300px, 62vw);
  border-radius: 50%;
  background: radial-gradient(circle at 30% 25%, var(--accent-soft), var(--surface) 72%);
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
  color: var(--accent);
  touch-action: manipulation;
}
.counter:active {
  transform: scale(0.94);
  box-shadow: 0 0 0 10px color-mix(in srgb, var(--accent) 10%, transparent);
}
/* One-shot per-tap pulse + completion flash (classes from pulseCounter/flashCounter). */
.counter.push { animation: counter-push 0.16s ease-out; }
.counter.win { animation: counter-win 0.55s ease-out; }
@keyframes counter-push {
  from { transform: scale(0.93); }
  to { transform: scale(1); }
}
@keyframes counter-win {
  0% { transform: scale(0.97); box-shadow: 0 0 0 4px color-mix(in srgb, var(--success) 55%, transparent); }
  35% { transform: scale(1.06); box-shadow: 0 0 0 26px color-mix(in srgb, var(--success) 28%, transparent); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 color-mix(in srgb, var(--success) 0%, transparent); }
}
.counter-value {
  position: relative;
  z-index: 1;
  font-size: clamp(52px, 19vw, 92px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.counter.done .counter-value { color: var(--success); }
.counter-ring {
  position: absolute;
  /* Encircles the button. Geometry: the svg box is the button + 28 px (inset
     −14); ring radius 0.46 × box lands the band ON the rim for every button
     size (overlap = 0.92·inset − 0.04·W). Too-tight insets hide the ring under
     the button collar on large screens. */
  top: -14px;
  left: -14px;
  width: calc(100% + 28px);
  height: calc(100% + 28px);
  transform: rotate(-90deg);
}
.counter-ring-track {
  fill: none;
  /* Deliberately visible: the empty track IS the circular progress bar frame. */
  stroke: var(--border);
  stroke-width: 8;
}
.counter-ring-bar {
  fill: none;
  stroke: var(--accent);
  stroke-width: 8;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.25s ease;
  /* soft glow so the progress arc pops against the button's gradient */
  filter: drop-shadow(0 0 3px color-mix(in srgb, var(--accent) 55%, transparent));
}
.counter.done .counter-ring-bar { stroke: var(--success); }
.counter.indeterminate .counter-ring-bar {
  animation: counter-spin 2.4s linear infinite;
  transform-box: fill-box;
  transform-origin: center;
}
@keyframes counter-spin { to { transform: rotate(360deg); } }
.counter-note { color: var(--fg-dim); font-size: 16px; margin-top:16px; }

.counter-bottom {
  flex-shrink: 0;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.counter-endtext { text-align: center; color: var(--fg-dim); font-size: 13px; white-space: pre-wrap; }
.counter-overrides { display: flex; gap: 10px; justify-content: center; }
.counter-overrides .icon-btn { width: 56px; height: 44px; font-size: 20px; }
.counter-next-row { display: flex; align-items: center; gap: 10px; }
.counter-quote {
  margin: 14px auto 0;
  max-width: 30em;
  text-align: center;
  font-size: 17px;
  font-style: italic;
  line-height: 1.6;
  color: var(--fg);
  padding: 0 6px;
}
.counter-quote::before {
  content: "❝";
  display: block;
  font-size: 30px;
  font-style: normal;
  color: var(--accent);
  margin-bottom: 6px;
}
.counter-next-info { flex: 1; font-size: 12px; color: var(--fg-dim); min-width: 0; }
.counter-next-info .n { font-weight: 700; color: var(--fg); }

/* ---------- session list panel (counter) ---------- */

.counter-list {
  flex-shrink: 0;
  /* kept within 140–480 px by the drag clamp — no max-height, or the resize
     would stop growing at 45vh while the cursor keeps moving */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.counter-list-body { overflow-y: auto; flex: 1 1 auto; min-height: 0; padding: 10px 14px 16px; }
.counter-list-title { font-size: 12px; font-weight: 800; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 8px; }
.leaf-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  width: 100%;
  text-align: left;
}
.leaf-row:hover { background: var(--surface); }
.leaf-row.active { background: var(--accent-soft); border-color: var(--accent); }
.leaf-group { font-size: 11px; font-weight: 800; color: var(--accent); text-transform: uppercase; letter-spacing: 0.06em; margin: 10px 0 2px; }
.leaf-icon { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border); display: flex; align-items: center; justify-content: center; font-size: 11px; flex-shrink: 0; color: transparent; }
.leaf-row.done .leaf-icon { border-color: var(--success); color: var(--success); }
.leaf-label { flex: 1; min-width: 0; font-size: 13px; }
.leaf-label .li-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.leaf-label .li-sub { color: var(--fg-dim); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.leaf-count { font-size: 12px; color: var(--fg-dim); font-variant-numeric: tabular-nums; }

/* ---------- finished overlay ---------- */

.finished {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 24px;
}
.finished-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: var(--shadow);
}
.finished-card .big { font-size: 44px; margin-bottom: 8px; }
.finished-card h2 { font-size: 20px; margin-bottom: 6px; }
.finished-card p { color: var(--fg-dim); font-size: 14px; margin-bottom: 16px; }
.finished-card .btn { margin-top: 6px; }

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

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.52);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  width: 100%;
  max-width: 340px;
}
.dialog-title { font-weight: 800; font-size: 16px; margin-bottom: 6px; }
.dialog-msg { color: var(--fg-dim); font-size: 14px; margin-bottom: 16px; white-space: pre-wrap; word-break: break-word; }
.dialog-actions { display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
.dialog-actions .btn { width: 100%; min-width: 0; white-space: nowrap; }

.toasts {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 14px);
  z-index: 110;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: calc(100% - 32px);
  max-width: 420px;
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: var(--radius);
  padding: 11px 14px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in 0.2s ease;
}
.toast .toast-msg { flex: 1; }
.toast .btn { white-space: nowrap; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
