:root {
  --bg: #0b0b0c;
  --bg-2: #141416;
  --card: #1c1c1f;
  --card-2: #242428;
  --line: #2e2e33;
  --text: #f4f4f5;
  --muted: #8d8d93;
  --dim: #636366;
  --lime: #d6ff32;
  --lime-2: #c4ef1c;
  --olive: #6b7a1f;
  --olive-fill: #3d4a12;
  --danger: #ff5a5a;
  --ok: #32d74b;
  --sheet: #161618;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --pad: 14px;
  --tap: 44px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html {
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  height: 100%;
  height: 100dvh;
  min-height: 100svh;
  overflow: hidden;
  overflow-x: hidden;
  overscroll-behavior: none;
  overscroll-behavior-y: contain;
  touch-action: manipulation;
}
button, input, select, textarea { font-family: inherit; color: inherit; font-size: 16px; }
button { cursor: pointer; border: 0; background: none; touch-action: manipulation; }
img { display: block; max-width: 100%; }
.hidden { display: none !important; }

#app {
  width: 100%;
  max-width: 430px;
  margin: 0 auto;
  height: 100%;
  height: 100dvh;
  min-height: 100svh;
  background: var(--bg);
  position: relative;
  overflow: hidden;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-top: var(--safe-top);
}

.scroll { flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px 8px;
  gap: 10px;
  min-height: 56px;
}
.topbar h1 { font-size: 17px; font-weight: 650; letter-spacing: -0.02em; text-align: center; flex: 1; }
.subhead { display: block; font-size: 13px; font-weight: 650; color: var(--lime); margin-top: 2px; }
.icon-btn {
  width: var(--tap); height: var(--tap); border-radius: 50%;
  background: var(--card); color: var(--text);
  display: grid; place-items: center; flex-shrink: 0;
}
.icon-btn.lime { background: var(--lime); color: #111; }
.icon-btn.ghost { background: transparent; border: 1px solid var(--line); }
.icon-btn svg { width: 18px; height: 18px; }

/* Week */
.week-list { padding: 4px var(--pad) 120px; display: flex; flex-direction: column; gap: 10px; }
.day-row { display: flex; gap: 10px; align-items: stretch; }
.day-rail {
  width: 44px; flex-shrink: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
}
.day-name { font-size: 12px; color: var(--muted); font-weight: 560; }
.day-num {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--card); display: grid; place-items: center;
  font-size: 14px; font-weight: 650;
}
.day-row.today .day-name { color: var(--lime); }
.day-row.today .day-num { background: var(--olive); color: var(--lime); }

.wcard {
  flex: 1; background: var(--card); border-radius: 16px;
  padding: 10px 10px 10px 10px; display: flex; gap: 10px; align-items: center;
  min-height: 78px; position: relative;
}
.wcard.rest {
  border: 1.5px dashed #3a3a40; background: transparent;
}
.thumb {
  width: 56px; height: 56px; border-radius: 10px; flex-shrink: 0;
  background: #2a2a30; display: grid; place-items: center;
  font-size: 26px; overflow: hidden;
}
.wmeta { flex: 1; min-width: 0; }
.wmeta-top { display: flex; align-items: center; gap: 6px; margin-bottom: 3px; flex-wrap: wrap; }
.prio {
  font-size: 11px; font-weight: 700; background: #2b3a10; color: var(--lime);
  padding: 2px 7px; border-radius: 6px; display: inline-flex; align-items: center; gap: 3px;
}
.dur { font-size: 12px; color: var(--muted); }
.wtitle { font-size: 16px; font-weight: 700; letter-spacing: -0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wsub { font-size: 13px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.more-btn { width: var(--tap); height: var(--tap); color: var(--muted); flex-shrink: 0; display: grid; place-items: center; }

.week-foot {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 10px var(--pad) calc(14px + var(--safe-bot));
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.pill-ghost {
  border: 1px solid var(--line); border-radius: 22px;
  padding: 10px 16px; font-size: 14px; font-weight: 600; color: var(--text);
}
.undo-row { display: flex; gap: 8px; }
.undo-row .icon-btn { width: 44px; height: 44px; }

/* Tabs */
.tabs {
  display: flex; gap: 0; padding: 0 8px; border-bottom: 1px solid var(--line);
}
.tab {
  flex: 1; padding: 12px 8px; font-size: 13px; font-weight: 650;
  color: var(--muted); border-bottom: 2px solid transparent;
}
.tab.on { color: var(--text); border-bottom-color: var(--lime); }

/* Library */
.lib-list { padding: 12px 14px 110px; display: flex; flex-direction: column; gap: 10px; }
.fab {
  position: absolute; right: 16px; bottom: calc(18px + var(--safe-bot));
  height: 52px; padding: 0 20px; border-radius: 26px;
  background: var(--lime); color: #111; font-weight: 800; font-size: 15px;
  display: flex; align-items: center; gap: 8px;
  box-shadow: 0 8px 24px rgba(214,255,50,.22);
}

/* Preview */
.preview-head { padding: 4px 16px 12px; }
.preview-title { font-size: clamp(22px, 7vw, 28px); font-weight: 800; letter-spacing: -0.03em; }
.preview-meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.section { padding: 8px 16px 18px; }
.section-h {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 22px; font-weight: 800; letter-spacing: -0.03em;
  padding: 10px 0 12px; border-top: 1px solid var(--line);
}
.ex-row {
  display: flex; gap: 12px; align-items: center; padding: 10px 0;
}
.ex-row .thumb { width: 64px; height: 64px; font-size: 28px; }
.ex-name { font-size: 16px; font-weight: 650; }
.ex-bits { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.chip {
  background: var(--card-2); border-radius: 8px; padding: 4px 8px;
  font-size: 12px; color: var(--text); font-weight: 560;
  display: inline-flex; align-items: center; gap: 4px;
}
.presc { font-size: 13px; color: var(--muted); }
.start-wrap {
  padding: 12px 16px calc(18px + var(--safe-bot));
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.start-btn {
  width: 100%; height: 56px; border-radius: 28px;
  background: var(--lime); color: #111; font-weight: 800; font-size: 16px; letter-spacing: 0.04em;
}

/* Builder */
.form { padding: 8px 16px 120px; display: flex; flex-direction: column; gap: 14px; }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 650; }
.field input, .field select, .field textarea {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 12px; font-size: 16px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--lime); }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sec-block {
  background: var(--card); border-radius: 16px; padding: 12px;
}
.sec-block h3 { font-size: 15px; margin-bottom: 8px; }
.ex-edit {
  background: var(--bg-2); border-radius: 12px; padding: 10px; margin-top: 8px;
}
.ex-edit .name { font-weight: 650; font-size: 14px; }
.mini-row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.mini-row input, .mini-row select {
  background: var(--card); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 8px; font-size: 16px; min-width: 0; flex: 1 1 88px;
}
.add-line {
  width: 100%; margin-top: 8px; padding: 10px; border-radius: 10px;
  border: 1px dashed var(--line); color: var(--muted); font-weight: 650; font-size: 13px;
}
.danger-text { color: var(--danger); font-size: 13px; font-weight: 650; }

/* Player */
.player { background: #000; }
.player-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px var(--pad);
  gap: 8px;
}
.elapsed { font-variant-numeric: tabular-nums; font-weight: 800; font-size: clamp(15px, 4.4vw, 18px); letter-spacing: -0.02em; flex: 1; text-align: center; }
.elapsed span { color: var(--muted); font-weight: 650; margin-left: 6px; font-size: 0.9em; }
.skip-sec { font-size: 13px; font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 6px; min-height: var(--tap); flex-shrink: 0; }
.pips { display: flex; gap: 4px; padding: 4px 16px 10px; }
.pip { flex: 1; height: 3px; background: #2a2a2a; border-radius: 2px; }
.pip.done { background: var(--lime); }
.pip.now { background: var(--lime); box-shadow: 0 0 8px rgba(214,255,50,.45); }
.player-body { flex: 1; overflow-y: auto; padding: 8px 16px 8px; }
.p-sec-title { font-size: clamp(22px, 7vw, 28px); font-weight: 800; letter-spacing: -0.03em; margin: 6px 0 10px; }
.p-item { padding: 12px 10px; border-radius: 10px; margin-bottom: 2px; }
.p-item.now {
  background: linear-gradient(90deg, #2a3a00 0%, #1a2200 70%, transparent);
  border-left: 3px solid var(--lime);
}
.p-item .n { font-weight: 700; font-size: 16px; }
.p-item .s { color: var(--muted); font-size: 13px; margin-top: 2px; }
.p-item .n em { color: var(--lime); font-style: normal; margin-right: 6px; }
.player-bot {
  padding: 8px 16px calc(12px + var(--safe-bot));
  display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: end;
}
.big-time {
  font-size: clamp(44px, 18vw, 72px); font-weight: 800; letter-spacing: -0.05em;
  font-variant-numeric: tabular-nums; line-height: 0.9;
  min-width: 0;
}
.ring-btn {
  width: 72px; height: 72px; border-radius: 50%;
  background: #2a2a2e; display: grid; place-items: center; position: relative;
}
.ring-btn svg.ring { position: absolute; inset: -3px; width: 78px; height: 78px; transform: rotate(-90deg); }
.journal-bar {
  grid-column: 1 / -1;
  background: #222226; border-radius: 16px; padding: 14px 16px;
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 700;
}
.journal-bar .tr { color: var(--lime); font-size: 13px; }
.warn {
  background: #2a2208; color: #f0d56a; font-size: 12px; font-weight: 650;
  padding: 8px 12px; display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.skip-row { display: flex; gap: 8px; padding: 0 var(--pad) 8px; }
.skip-row button {
  flex: 1; background: var(--card); border-radius: 12px; padding: 12px 6px;
  min-height: var(--tap); font-size: 13px; font-weight: 700;
}

/* Journal sheet */
.sheet-bg {
  position: absolute; inset: 0; background: rgba(0,0,0,.55);
  z-index: 20; display: flex; flex-direction: column; justify-content: flex-end;
}
.sheet {
  background: var(--sheet); border-radius: 20px 20px 0 0;
  max-height: 88%; display: flex; flex-direction: column;
  padding-bottom: var(--safe-bot);
}
.sheet-h {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 8px;
}
.sheet-h h2 { font-size: 18px; font-weight: 800; }
.sheet-body { overflow-y: auto; padding: 0 12px 12px; }
.j-card { background: var(--card); border-radius: 16px; padding: 12px; margin-bottom: 10px; }
.j-name { font-size: 16px; font-weight: 700; margin-bottom: 8px; }
.j-tools { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; }
.j-tools .chip { white-space: nowrap; }
.j-cols, .j-set {
  display: grid;
  grid-template-columns: 28px minmax(0,1fr) minmax(0,1fr) minmax(0,1fr) minmax(0,1.1fr);
  gap: 6px;
  align-items: center;
}
.j-cols { font-size: 11px; color: var(--muted); font-weight: 650; padding: 4px 0; }
.j-set { margin-bottom: 6px; }
.j-set.on { background: #2a320a; border-radius: 10px; padding: 4px 2px; }
.set-n {
  width: 24px; height: 24px; border-radius: 50%; border: 1.5px solid var(--text);
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
}
.j-set input {
  width: 100%; background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 4px; text-align: center; font-size: 16px; font-weight: 650; min-width: 0;
}
.sheet-foot {
  padding: 10px 16px 12px; display: flex; align-items: center; justify-content: space-between;
  border-top: 1px solid var(--line);
}
.pct { font-size: 13px; color: var(--muted); text-align: center; flex: 1; }
.pct b { color: var(--lime); display: block; }

/* History */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 8px var(--pad) 14px; }
.stat { background: var(--card); border-radius: 12px; padding: 10px 6px; text-align: center; }
.stat b { display: block; font-size: 15px; font-weight: 800; }
.stat span { font-size: 10px; color: var(--muted); }
.hist-h { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 0 var(--pad) 24px; }
.table { width: 100%; min-width: 480px; border-collapse: collapse; font-size: 13px; }
.table th { text-align: left; color: var(--muted); font-weight: 650; font-size: 11px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.table td { padding: 10px 10px; border-bottom: 1px solid var(--line); }

/* Menus */
.menu {
  position: absolute; z-index: 30; background: var(--card-2);
  border-radius: 14px; min-width: 180px; padding: 6px;
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
}
.menu button { display: block; width: 100%; text-align: left; padding: 12px 12px; border-radius: 10px; font-size: 15px; font-weight: 560; }
.menu button:hover { background: #333; }
.toast {
  position: absolute; left: 50%; bottom: calc(24px + var(--safe-bot)); transform: translateX(-50%);
  background: #eee; color: #111; padding: 10px 16px; border-radius: 20px;
  font-size: 13px; font-weight: 700; z-index: 50; white-space: nowrap;
}

/* Unit toggle */
.toggle { display: inline-flex; background: var(--card); border-radius: 8px; overflow: hidden; }
.toggle button { padding: 6px 10px; font-size: 12px; font-weight: 700; color: var(--muted); }
.toggle button.on { background: var(--lime); color: #111; }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.empty h3 { color: var(--text); margin-bottom: 6px; }

.picker-list { max-height: 60vh; overflow: auto; padding: 8px; }
.picker-list button {
  display: flex; width: 100%; text-align: left; padding: 12px; gap: 10px; align-items: center;
  border-radius: 12px;
}
.picker-list button:hover { background: var(--card); }

.sheet { max-height: min(88%, var(--vvh, 88dvh)); }

@media (max-width: 359px) {
  .day-rail { width: 36px; }
  .day-num { width: 30px; height: 30px; font-size: 13px; }
  .thumb { width: 48px; height: 48px; font-size: 22px; }
  .ex-row .thumb { width: 52px; height: 52px; }
  .wtitle { font-size: 15px; }
  .week-foot { flex-wrap: wrap; }
}

@media (min-width: 380px) {
  .stats { grid-template-columns: 1fr 1fr 1fr 1fr; }
}

@media (min-width: 600px) {
  #app { max-width: 480px; box-shadow: 0 0 0 1px #111, 0 24px 80px rgba(0,0,0,.45); }
}

@media (min-width: 900px) {
  #app { max-width: 520px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  .topbar { min-height: 44px; padding-top: 4px; padding-bottom: 4px; }
  .preview-title, .p-sec-title { font-size: 20px; }
  .big-time { font-size: 40px; }
  .ring-btn { width: 52px; height: 52px; }
  .ring-btn svg.ring { width: 58px; height: 58px; inset: -3px; }
  .player-bot { padding-top: 0; }
  .warn { display: none; }
  .sheet { max-height: 94%; }
  .ex-row .thumb, .player .thumb { display: none; }
}

@media (hover: none) {
  .menu button:hover,
  .picker-list button:hover { background: transparent; }
}

@media (display-mode: standalone) {
  .view { padding-top: max(var(--safe-top), 8px); }
}
