:root {
  --bg: #171a26;
  --panel: #1f2333;
  --panel-2: #262b3f;
  --raise: #2e3449;
  --line: #333a52;
  --text: #eceaf4;
  --muted: #9095ae;
  --accent: #ffd447;      /* สีเหลืองซับคลาสสิก */
  --accent-ink: #1d1806;
  --speech: #8fb0ff;
  --danger: #ff6f6f;
  --ok: #5fe0aa;
  --tl-bg: #1a1e2c;
  --tl-wave: #3e4766;
  --tl-wave-on: #8fb0ff;
  --tl-cap: #343b57;
  --radius: 10px;
  --font: "Anuphan", "Noto Sans Thai", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app { display: grid; grid-template-rows: auto 1fr; height: 100vh; height: 100dvh; }

/* ---------- แถบบน ---------- */
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--bg); min-height: 58px; }
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--panel-2); position: relative; display: block; }
.brand-mark i, .brand-mark b { position: absolute; left: 6px; right: 6px; border-radius: 2px; }
.brand-mark i { top: 9px; height: 4px; background: var(--text); right: 10px; }
.brand-mark b { bottom: 7px; height: 7px; background: var(--accent); }
.brand-name { font-family: "Kanit", sans-serif; font-weight: 800; font-style: italic; font-size: 22px; letter-spacing: 0.01em; }
.file-meta { flex: 1; min-width: 0; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-meta b { color: var(--text); font-weight: 600; margin-right: 8px; }
.top-actions { display: flex; gap: 8px; align-items: center; }

/* ---------- ปุ่ม ---------- */
.btn { border: 1px solid transparent; border-radius: 9px; padding: 9px 16px; cursor: pointer; background: var(--raise); font-weight: 600; transition: background .15s, transform .05s; }
.btn:hover { background: #384060; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #ffe07a; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); border-color: #46506f; }
.btn.sm { padding: 5px 11px; font-size: 13px; border-radius: 7px; }
.btn.big { padding: 13px 30px; font-size: 17px; border-radius: 12px; }
.btn.block { width: 100%; }
.icon-btn { width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--line); background: transparent; display: grid; place-items: center; cursor: pointer; }
.icon-btn:hover:not(:disabled) { background: var(--panel-2); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.sm { width: 30px; height: 30px; }
.icon-btn.sm svg { width: 15px; height: 15px; }

/* ---------- หน้าแรก ---------- */
.landing { padding: clamp(16px, 4vw, 48px); display: grid; align-content: center; justify-items: center; gap: 18px; overflow: auto; }
.drop { position: relative; width: min(1120px, 100%); display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); gap: clamp(20px, 4vw, 56px); align-items: center;
  border-radius: 22px; padding: clamp(24px, 4.5vw, 56px);
  background: radial-gradient(120% 90% at 30% 0%, #283050 0%, #1b1f30 55%, #151826 100%); border: 1.5px dashed #414a6b; cursor: pointer; transition: border-color .2s; }
.drop:hover, .drop.over { border-color: var(--accent); }
.screen { order: 2; position: relative; aspect-ratio: 16 / 9; border-radius: 14px; overflow: hidden; box-shadow: 0 24px 60px rgba(0,0,0,.45);
  background: radial-gradient(90% 120% at 70% 20%, #6b5a8f 0%, #3a3f66 40%, #1e2236 100%); }
.screen::after { content: ""; position: absolute; inset: 0; background: radial-gradient(40% 50% at 30% 75%, rgba(255,190,120,.28), transparent 70%); pointer-events: none; }
.demo { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; }
.screen-bar { position: absolute; left: 0; right: 0; bottom: 0; height: 26px; display: flex; align-items: center; gap: 10px; padding: 0 12px; z-index: 2;
  background: linear-gradient(transparent, rgba(0,0,0,.55)); font-size: 11px; color: #d5d3e0; }
.screen-bar i { flex: 1; height: 3px; border-radius: 3px; background: linear-gradient(90deg, var(--accent) 38%, rgba(255,255,255,.25) 38%); }
.drop-copy { order: 1; display: grid; align-content: center; justify-items: start; gap: 10px; }
.drop h1 { font-family: "Kanit", sans-serif; font-weight: 700; font-size: clamp(30px, 3.6vw, 48px); line-height: 1.15; margin: 0 0 6px; text-wrap: balance; }
.drop p { margin: 0; color: var(--muted); }
.facts { list-style: none; margin: 18px 0 0; padding: 0; display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.facts strong { color: var(--text); font-weight: 600; margin-right: 6px; }
.compat { width: min(1040px, 100%); display: flex; flex-wrap: wrap; gap: 8px; font-size: 13px; }
.compat span { padding: 5px 11px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); }
.compat span.ok::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); margin-right: 7px; vertical-align: 1px; }
.compat span.bad { color: #ffc4c4; border-color: #6a3a42; }
.compat span.bad::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--danger); margin-right: 7px; vertical-align: 1px; }

/* ---------- พื้นที่ทำงาน ---------- */
.workspace { display: grid; grid-template-columns: minmax(0, 1fr) 400px; min-height: 0; }
.stage-col { display: grid; grid-template-rows: minmax(0, 1fr) auto 128px; min-width: 0; min-height: 0; }
.stage { display: grid; place-items: center; min-height: 0; padding: 16px; background:
  repeating-conic-gradient(#191c29 0 25%, #1b1f2d 0 50%) 0 0 / 22px 22px; }
.frame { position: relative; max-width: 100%; max-height: 100%; line-height: 0; box-shadow: 0 12px 40px rgba(0,0,0,.45); }
.frame video { display: block; max-width: 100%; max-height: calc(100dvh - 58px - 32px - 52px - 128px); background: #000; }
.overlay { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.overlay.grab { cursor: grab; }
.overlay.grabbing { cursor: grabbing; }
.drag-hint { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); line-height: 1.4; font-size: 12px; padding: 4px 10px; border-radius: 999px; background: rgba(0,0,0,.6); opacity: 0; transition: opacity .2s; pointer-events: none; }
.frame:hover .drag-hint.show { opacity: 1; }

.transport { display: flex; align-items: center; gap: 12px; padding: 8px 14px; border-top: 1px solid var(--line); background: var(--panel); }
.transport .play { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); border: none; }
.transport .play svg { fill: currentColor; stroke: none; width: 18px; height: 18px; }
.transport .play:hover { background: #ffe07a; }
.i-pause { display: none; }
.playing .i-pause { display: block; }
.playing .i-play { display: none; }
.timecode { font-variant-numeric: tabular-nums; font-size: 14px; color: var(--muted); }
.timecode b { color: var(--text); font-weight: 600; }
.spacer { flex: 1; }
.mini { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.mini select { background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px; padding: 3px 6px; }
.chip-toggle { border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 12px; font-size: 13px; cursor: pointer; color: var(--muted); }
.chip-toggle[aria-pressed="true"] { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
.zoom { display: flex; gap: 4px; }
.timeline { width: 100%; height: 128px; display: block; border-top: 1px solid var(--line); touch-action: none; }

/* ---------- แผงด้านขวา ---------- */
.panel { border-left: 1px solid var(--line); background: var(--panel); display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; }
.tabs { display: flex; padding: 6px 8px 0; gap: 2px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs button { border: none; background: none; padding: 10px 11px 11px; cursor: pointer; color: var(--muted); font-weight: 600; font-size: 14px; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tabs .count { font-size: 11px; background: var(--raise); padding: 1px 6px; border-radius: 999px; margin-left: 2px; font-weight: 500; }
.tabs .count:empty { display: none; }
.tab-body { overflow-y: auto; padding: 16px 18px 40px; min-height: 0; }
.tab-body h3 { font-size: 13px; font-weight: 600; color: var(--muted); margin: 22px 0 10px; }
.tab-body h3:first-child { margin-top: 4px; }

.field { display: grid; gap: 5px; font-size: 13px; color: var(--muted); margin-bottom: 10px; min-width: 0; }
.field > span { display: flex; justify-content: space-between; gap: 8px; }
.field small { color: #6f7590; font-size: 12px; }
.field output { color: var(--text); font-variant-numeric: tabular-nums; }
.field input[type="text"], .field input[type="search"], .field select, .field textarea, .search input, select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; width: 100%; color: var(--text); }
.field textarea { resize: vertical; min-height: 44px; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="color"] { width: 100%; height: 34px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); padding: 3px; cursor: pointer; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.span-2 { grid-column: span 2; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.row-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.seg.wrap { flex-wrap: wrap; }
.seg button { flex: 1 1 auto; border: none; background: none; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--muted); white-space: nowrap; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--raise); color: var(--text); box-shadow: inset 0 0 0 1px #4a5376; }
.switch { display: flex; align-items: center; gap: 9px; margin: 4px 0 10px; cursor: pointer; font-size: 14px; }
.checks { display: grid; gap: 6px; margin: 0 0 12px; font-size: 14px; }
.checks label { display: flex; gap: 8px; align-items: center; cursor: pointer; }

.device { font-size: 13px; padding: 10px 12px; border-radius: 9px; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.device b { color: var(--text); font-weight: 600; }
.device.warn { border-color: #6c5a2c; color: #f1dd9f; }

.model-list { display: grid; gap: 8px; }
.model { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; align-items: baseline; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; background: var(--bg); }
.model:has(input:checked) { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.model input { grid-row: span 2; align-self: center; }
.m-name { font-weight: 600; }
.m-desc { grid-column: 2 / 4; font-size: 12.5px; color: var(--muted); }
.m-size { grid-column: 3; grid-row: 1; font-size: 12px; color: var(--muted); white-space: nowrap; }
.adv { margin: 14px 0; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; }
.adv summary { cursor: pointer; padding: 10px 0; font-size: 14px; color: var(--muted); }
.adv[open] { padding-bottom: 6px; }

.progress-wrap { margin-top: 14px; padding: 12px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.p-row { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px; }
.p-row span:last-child { font-variant-numeric: tabular-nums; color: var(--muted); }
.bar { height: 6px; border-radius: 99px; background: var(--raise); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .25s; }
.bar.indet i { width: 35% !important; animation: indet 1.2s ease-in-out infinite; }
@keyframes indet { from { transform: translateX(-100%); } to { transform: translateX(300%); } }
.p-note { font-size: 12.5px; color: var(--muted); margin: 8px 0 8px; }
.live { margin-top: 10px; font-size: 14px; color: var(--muted); max-height: 140px; overflow: hidden; mask-image: linear-gradient(to top, #000 60%, transparent); }
.live p { margin: 0 0 3px; }
.live p:last-child { color: var(--text); }
.or { text-align: center; color: var(--muted); font-size: 12px; margin: 22px 0 4px; position: relative; }
.or::before, .or::after { content: ""; position: absolute; top: 50%; width: 40%; height: 1px; background: var(--line); }
.or::before { left: 0; } .or::after { right: 0; }

/* ---------- รายการซับ ---------- */
.edit-tools { position: sticky; top: -16px; background: var(--panel); padding: 2px 0 10px; z-index: 2; margin-top: -2px; }
.search { display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px; }
.search input { padding: 6px 9px; font-size: 13px; }
.row-tools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.shift { display: flex; gap: 5px; align-items: center; font-size: 13px; color: var(--muted); margin-left: auto; }
.shift input { width: 60px; background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 4px 6px; font-size: 13px; }
.hint { font-size: 12px; color: #737a96; margin: 8px 0 0; }
.cap-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.cap { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px 9px; background: var(--bg); }
.cap.sel { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.cap.now .c-idx { background: var(--accent); color: var(--accent-ink); }
.c-head { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.c-idx { min-width: 26px; text-align: center; border-radius: 6px; background: var(--raise); padding: 0 5px; font-variant-numeric: tabular-nums; font-size: 12px; }
.c-time { width: 76px; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 1px 5px; font-variant-numeric: tabular-nums; font-size: 12.5px; color: var(--muted); }
.c-time:hover, .c-time:focus { border-color: var(--line); color: var(--text); }
.c-dur { margin-right: auto; font-size: 11.5px; }
.c-dur.warn { color: #ffb36b; }
.c-act { display: flex; gap: 2px; opacity: .55; }
.cap:hover .c-act, .cap.sel .c-act { opacity: 1; }
.c-act button { border: none; background: none; cursor: pointer; padding: 3px 5px; border-radius: 5px; color: var(--muted); font-size: 12px; }
.c-act button:hover { background: var(--raise); color: var(--text); }
.c-text { field-sizing: content; min-height: 1.6em; width: 100%; resize: none; border: none; background: transparent; font-size: 15.5px; line-height: 1.5; padding: 4px 0 2px; color: var(--text); overflow: hidden; }
.c-text:focus { outline: none; }
.c-words { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 3px; }
.w { font-size: 12.5px; padding: 1px 6px; border-radius: 5px; background: var(--panel-2); cursor: pointer; border: 1px solid transparent; user-select: none; }
.w.auto { color: var(--accent); border-color: rgba(255,212,71,.35); }
.w.on { background: var(--accent); color: var(--accent-ink); }
.w.off { text-decoration: line-through; opacity: .55; }
.empty-note { color: var(--muted); font-size: 14px; padding: 28px 8px; text-align: center; }

/* ---------- สไตล์ ---------- */
.preset-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.preset { position: relative; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; cursor: pointer; background: #0f1220; padding: 0; text-align: left; }
.preset canvas { display: block; width: 100%; aspect-ratio: 16 / 9; background: linear-gradient(160deg, #4a5578 0%, #2a3048 55%, #1b2033 100%); }
.preset span { position: absolute; left: 8px; top: 6px; font-size: 12px; line-height: 1.3; padding: 2px 7px; border-radius: 6px; background: rgba(10,12,20,.6); }
.preset.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.pos-presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.pos-presets button { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 6px 0; cursor: pointer; font-size: 13px; color: var(--muted); }
.pos-presets button:hover { color: var(--text); border-color: #4a5376; }
.my-styles { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.my-styles .chip { display: inline-flex; gap: 6px; align-items: center; border: 1px solid var(--line); border-radius: 999px; padding: 3px 4px 3px 12px; font-size: 13px; cursor: pointer; background: var(--bg); }
.my-styles .chip button { border: none; background: var(--raise); width: 20px; height: 20px; border-radius: 50%; cursor: pointer; color: var(--muted); line-height: 1; }

/* ---------- ส่งออก ---------- */
.src-info { font-size: 13px; color: var(--muted); padding: 10px 12px; border-radius: 9px; background: var(--bg); border: 1px solid var(--line); margin-bottom: 14px; display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; }
.src-info b { color: var(--text); font-weight: 500; }
.ex-card { border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 12px; background: var(--bg); }
.ex-card h3 { margin: 0 0 4px !important; color: var(--text) !important; font-size: 15px !important; }
.ex-card p { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.ex-card .q-note { margin-top: -4px; font-size: 12.5px; }
.ex-card .row-2 { margin-top: 8px; }

/* ---------- แจ้งเตือน ---------- */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #f3f1fa; color: #171a26; padding: 10px 18px; border-radius: 10px; font-weight: 500; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: min(92vw, 560px); box-shadow: 0 10px 30px rgba(0,0,0,.4); z-index: 50; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: #ffd6d6; }
.dropveil { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(15,17,27,.82); font-family: "Kanit"; font-size: 32px; font-weight: 600; color: var(--accent); opacity: 0; pointer-events: none; transition: opacity .15s; z-index: 60; border: 3px dashed var(--accent); }
.dropveil.show { opacity: 1; }

/* ---------- จอเล็ก ---------- */
@media (max-width: 980px) {
  .app { height: auto; min-height: 100dvh; }
  .workspace { grid-template-columns: 1fr; }
  .stage-col { grid-template-rows: auto auto 110px; }
  .frame video { max-height: 56vh; }
  .timeline { height: 110px; }
  .panel { border-left: none; border-top: 1px solid var(--line); }
  .tab-body { overflow: visible; }
  .drop { grid-template-columns: 1fr; }
  .screen { order: 0; }
}
@media (max-width: 560px) {
  .topbar { padding: 8px 12px; gap: 10px; }
  .file-meta { display: none; }
  .transport { flex-wrap: wrap; gap: 8px; }
  .mini span { display: none; }
  .search { grid-template-columns: 1fr 1fr; }
  .search .btn { grid-column: span 2; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

.btn, .brand-name { white-space: nowrap; }
@media (max-width: 560px) {
  #btnNew { display: none; }
  .brand-name { font-size: 19px; }
}

.storage { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 12.5px; color: var(--muted); margin: 4px 0 8px; }

.model-note { font-size: 12.5px; color: var(--muted); margin: -4px 0 10px; }
.model:has(input:disabled) { opacity: .45; cursor: not-allowed; }
