/* พร้อมพูด — styles (ops-dark base) */

:root {
  /* สีระบบ (semantic tokens) */
  --bg: #0F1011;
  --surface: #16181A;
  --surface-2: #1D2023;
  --border: #2A2E33;
  --text: #EDEEF0;
  --text-muted: #A8AEB8;
  --accent: #8FA7FF;
  --accent-strong: #B7C6FF;
  --accent-ink: #0F1011;
  --danger: #FF7A7A;
  --danger-soft: rgba(255, 122, 122, 0.12);

  /* สเปซ 4/8 grid */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s6: 24px;
  --s8: 32px;
  --s12: 48px;

  /* ตัวอักษร */
  --font-thai: "IBM Plex Sans Thai", "Noto Sans Thai", sans-serif;
  --font-mono: "IBM Plex Mono", monospace;
  --text-sm: 0.875rem;
  --text-md: 1rem;
  --text-lg: 1.125rem;

  --radius: 10px;
  --radius-sm: 6px;
  --dur: 180ms;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }

/* [hidden] ต้องชนะเสมอ — ไม่งั้น display:flex ของ .view/.player จะทำให้ทุกหน้าซ้อนกัน */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-thai);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100%;
}

body { min-height: 100dvh; }

/* ---------- ปุ่ม (4 ระดับ) ---------- */

.btn {
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: 500;
  min-height: 44px;
  padding: 0 var(--s4);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  justify-content: center;
  transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease),
    color var(--dur) var(--ease), transform var(--dur) var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-strong); }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover { background: #23262A; }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
}
.btn-ghost:hover { color: var(--text); background: var(--surface-2); }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: transparent;
}
.btn-danger:hover { background: var(--danger-soft); }

.btn-block { width: 100%; }

.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn[disabled]:active { transform: none; }

:is(button, input, a, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ---------- โครงหน้า ---------- */

.view { min-height: 100dvh; display: flex; flex-direction: column; }
.view-center { align-items: center; justify-content: center; padding: var(--s6); }

.surface {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* ล็อกอิน */
.login-card {
  width: min(400px, 100%);
  padding: var(--s8) var(--s6);
}
.brand {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin: 0;
}
.login-sub {
  color: var(--text-muted);
  margin: var(--s2) 0 var(--s6);
}

.field { display: flex; flex-direction: column; gap: var(--s2); margin-bottom: var(--s4); }
.field label { font-size: var(--text-sm); font-weight: 500; }

input[type="text"], input[type="password"], input[type="search"], textarea {
  font-family: inherit;
  font-size: var(--text-md);
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--s3) var(--s4);
  min-height: 44px;
  width: 100%;
}
input::placeholder, textarea::placeholder { color: #7A828E; }
input:focus, textarea:focus { border-color: var(--accent); outline: none; }
input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

textarea { resize: vertical; min-height: 40vh; line-height: 1.6; }

.form-error {
  color: var(--danger);
  font-size: var(--text-sm);
  margin: var(--s2) 0;
}

/* แถบบน */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  padding: var(--s3) var(--s4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand-sm { font-weight: 700; font-size: var(--text-lg); }
.topbar-right { display: flex; align-items: center; gap: var(--s3); }
.topbar-title { color: var(--text-muted); font-size: var(--text-sm); }
.user-chip {
  color: var(--text-muted);
  font-size: var(--text-sm);
  max-width: 30vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* หน้ารายการ */
.page {
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: var(--s6) var(--s4) var(--s12);
  flex: 1;
}
.list-toolbar {
  display: flex;
  gap: var(--s3);
  flex-wrap: wrap;
  margin-bottom: var(--s4);
}
.search-input { flex: 1; min-width: 220px; max-width: 480px; }
.list-status { color: var(--text-muted); font-size: var(--text-sm); margin: var(--s2) 0 var(--s4); }

.script-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}
.script-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--dur) var(--ease);
}
.script-item:hover { border-color: #3A4048; }
.script-info { min-width: 0; flex: 1; cursor: pointer; }
.script-title {
  font-size: var(--text-md);
  font-weight: 600;
  margin: 0 0 var(--s1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: none;
  border: none;
  color: var(--text);
  padding: 0;
  text-align: left;
  font-family: inherit;
  width: 100%;
}
.script-title:hover { color: var(--accent-strong); }
.script-meta {
  color: var(--text-muted);
  font-size: var(--text-sm);
  margin: 0;
  font-variant-numeric: tabular-nums;
}
.script-actions { display: flex; gap: var(--s2); flex-shrink: 0; }

/* empty state */
.empty-state {
  text-align: center;
  padding: var(--s12) var(--s4);
  color: var(--text-muted);
}
.empty-icon { font-size: 2.5rem; margin-bottom: var(--s3); }
.empty-state p { margin: 0 0 var(--s4); }
.empty-state .btn { margin: 0 auto; }

/* หน้าแก้ไข */
.page-editor { max-width: 860px; }
.editor-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  flex-wrap: wrap;
  margin-top: var(--s4);
}
.editor-count { color: var(--text-muted); font-size: var(--text-sm); font-variant-numeric: tabular-nums; }
.editor-actions { display: flex; gap: var(--s3); }

/* ---------- Player ---------- */

.player {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  background: #000000;
}
.player-stage {
  position: relative;
  flex: 1;
  overflow: hidden;
}
.player-content {
  position: absolute;
  inset-inline: 0;
  top: 0;
  padding: 50dvh var(--s6);
  will-change: transform;
}
.player-content.anim { transition: transform 160ms var(--ease); }
.pl-line {
  font-size: 80px;
  line-height: 1.35;
  font-weight: 600;
  text-align: center;
  padding: 0.35em 0;
  opacity: 0.35;
  transition: opacity 120ms linear;
}
.pl-line.current { opacity: 1; }
.player-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--text-muted);
  font-size: var(--text-lg);
}

.player-controls {
  background: rgba(15, 16, 17, 0.92);
  border-top: 1px solid var(--border);
  padding: var(--s2) var(--s3) calc(var(--s3) + env(safe-area-inset-bottom));
}
.player-controls-row {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex-wrap: wrap;
}
.player-controls-spacer { flex: 1; }
.btn-player {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
  min-width: 48px;
}
.btn-player:hover { background: #23262A; }
.btn-player[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}
.btn-play { font-weight: 600; min-width: 96px; }
.player-progress {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: var(--text-sm);
  color: var(--text-muted);
  min-width: 64px;
  text-align: center;
}

.player-settings {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s4) var(--s8);
  padding: var(--s3) var(--s2) 0;
}
.setting {
  display: flex;
  align-items: center;
  gap: var(--s2);
}
.setting label { font-size: var(--text-sm); color: var(--text-muted); white-space: nowrap; }
.setting input[type="range"] {
  width: 160px;
  accent-color: var(--accent);
  min-height: 44px;
}
.setting output {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  min-width: 96px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.setting input[type="color"] {
  width: 48px;
  height: 44px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  cursor: pointer;
}

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

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--s8);
  transform: translateX(-50%);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: var(--s3) var(--s4);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  z-index: 200;
  max-width: min(90vw, 420px);
  text-align: center;
}

/* ---------- มือถือ ---------- */

@media (max-width: 640px) {
  .script-item { flex-direction: column; align-items: stretch; }
  .script-actions { justify-content: flex-start; }
  .script-title { white-space: normal; }
  .setting input[type="range"] { width: 120px; }
  .setting output { min-width: 84px; }
  .player-content { padding-inline: var(--s4); }
}

/* ลดการเคลื่อนไหว */
@media (prefers-reduced-motion: reduce) {
  .player-content.anim { transition: none; }
  .btn:active { transform: none; }
  * { transition-duration: 0ms !important; animation-duration: 0ms !important; }
}
