:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --line: #e2e6ef;
  --primary: #3b5bfd;
  --primary-ink: #ffffff;
  --danger: #e5484d;
  --ok: #1f9d55;
  --warn: #b7791f;
  --iplay: #7c3aed;
  --bob: #0ea5e9;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
}
/* โหมดกลางคืน: ตามเครื่อง (ถ้ายังไม่ได้เลือกเอง) หรือเมื่อกดปุ่ม 🌙 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1320;
    --surface: #171c2c;
    --text: #e7eaf3;
    --muted: #8d95ab;
    --line: #262d42;
    --primary: #5b7bff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1320;
  --surface: #171c2c;
  --text: #e7eaf3;
  --muted: #8d95ab;
  --line: #262d42;
  --primary: #5b7bff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }

.theme-toggle {
  background: transparent; border: 1px solid var(--line); border-radius: 8px;
  width: 34px; height: 32px; font-size: 1rem; line-height: 1; cursor: pointer; color: var(--text);
}
.theme-toggle:hover { background: var(--bg); }
.theme-float { position: fixed; top: 14px; right: 16px; z-index: 20; }

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Prompt', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--primary); }
.muted { color: var(--muted); }
small { font-size: .82em; }
[hidden] { display: none !important; }

.wrap { max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.narrow { max-width: 460px; margin: 20px auto; }
h1, h2, h3 { margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 1.05rem; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; flex-wrap: wrap; gap: 10px 20px; align-items: center;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 600; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.topbar nav a {
  text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: 8px;
}
.topbar nav a.on, .topbar nav a:hover { background: var(--bg); color: var(--text); }
.who { display: flex; align-items: center; gap: 10px; }
.who form { margin: 0; }

/* ---------- forms ---------- */
label { display: block; margin-bottom: 12px; font-weight: 500; }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 9px 11px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text);
  font: inherit;
}
textarea { resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: 8px; padding: 9px 16px;
  font: inherit; font-weight: 500; cursor: pointer; background: var(--bg); color: var(--text);
}
.btn.sm { padding: 5px 11px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:not(:disabled):hover { filter: brightness(1.05); }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; border: 1px solid; }
.alert.ok { color: var(--ok); border-color: var(--ok); background: color-mix(in srgb, var(--ok) 10%, transparent); }
.alert.err { color: var(--danger); border-color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.alert.warn { color: var(--warn); border-color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, transparent); }

.login-box { max-width: 380px; margin: 10vh auto 0; }
.login-box h1 { font-size: 1.4rem; margin-bottom: 2px; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.tab {
  font: inherit; font-weight: 600; font-size: 1rem; cursor: pointer;
  padding: 10px 18px; border-radius: 10px; border: 2px solid var(--line);
  background: var(--surface); color: var(--muted);
}
.tab.on.shop-iplaytvx { border-color: var(--iplay); color: var(--iplay); }
.tab.on.shop-bob { border-color: var(--bob); color: var(--bob); }

.pill {
  display: inline-block; min-width: 26px; padding: 1px 9px; border-radius: 999px;
  font-size: .8rem; font-weight: 600; text-align: center; background: var(--bg); color: var(--muted);
}
.pill.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
.pill.empty { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }

.panel-note { margin: 0 0 12px; }
.pkg-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.pkg { border-top: 4px solid var(--line); padding: 14px; }
.shop-iplaytvx .pkg { border-top-color: var(--iplay); }
.shop-bob .pkg { border-top-color: var(--bob); }
.pkg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.pkg-head h3 { margin: 0; }
.empty-msg { margin: 6px 0; }

.items { list-style: none; margin: 0; padding: 0; max-height: 460px; overflow-y: auto; }
.item {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 9px 4px; border-bottom: 1px solid var(--line);
}
.item:last-child { border-bottom: 0; }
.item-info { display: flex; flex-direction: column; min-width: 0; }
.item-info .meta { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.item.done .item-info strong { color: var(--muted); text-decoration: line-through; }
.status { font-size: .82rem; font-weight: 600; }
.st-new { color: var(--ok); }
.st-part { color: var(--warn); }
.st-done { color: var(--muted); }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(10, 14, 25, .55); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal-box { width: 100%; max-width: 560px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { margin: 0; }
.x { background: none; border: 0; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  background: var(--ok); color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow);
  max-width: calc(100% - 32px);
}
.toast.err { background: var(--danger); }

/* ---------- admin ---------- */
.grid-2 { display: grid; gap: 16px; grid-template-columns: minmax(280px, 380px) minmax(0, 1fr); align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 120px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.filters label { margin: 0; flex: 1; min-width: 130px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; white-space: nowrap; }
td.content { max-width: 340px; white-space: pre-wrap; word-break: break-all; font-family: ui-monospace, Menlo, monospace; font-size: .82rem; }
td form { margin: 0; display: inline; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 6px; font-size: .78rem; font-weight: 600; color: #fff; }
.tag.iplaytvx { background: var(--iplay); }
.tag.bob { background: var(--bob); }
.hint { font-size: .85rem; color: var(--muted); margin: -6px 0 12px; }
.pager { display: flex; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.pager a, .pager span { padding: 4px 10px; border-radius: 6px; border: 1px solid var(--line); text-decoration: none; }
.pager span { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- embedded external page ---------- */
.embed-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.embed-bar span { display: flex; gap: 6px; }
.embed-bar a.btn { text-decoration: none; }
.embed-frame {
  display: block; width: 100%; height: calc(100vh - 150px); min-height: 520px;
  border: 1px solid var(--line); border-radius: var(--radius); background: #fff;
}

/* ---------- IBO tab ---------- */
.ibo-layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 14px; align-items: start; }
@media (max-width: 900px) { .ibo-layout { grid-template-columns: minmax(0, 1fr); } }
.ibo-side { display: flex; flex-direction: column; gap: 14px; }
.ibo-side .card { padding: 14px; }
.ibo-current { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line); }
.copy-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.copy-row code, .mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
.copy-row code { background: var(--bg); padding: 4px 8px; border-radius: 6px; overflow: hidden; text-overflow: ellipsis; }
.dev-list { list-style: none; margin: 0; padding: 0; max-height: 420px; overflow-y: auto; }
.dev-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 2px; border-bottom: 1px solid var(--line); }
.dev-list li:last-child { border-bottom: 0; }
.ibo-main .embed-frame { height: calc(100vh - 140px); }

/* ---------- OCR upload ---------- */
.ocr-drop {
  border: 2px dashed var(--line); border-radius: 10px; padding: 14px; margin-bottom: 12px;
  text-align: center; cursor: pointer; transition: border-color .15s, background .15s;
}
.ocr-drop:hover, .ocr-drop:focus, .ocr-drop.over { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 6%, transparent); outline: none; }
.ocr-drop img { display: block; max-width: 100%; max-height: 140px; margin: 10px auto 0; border-radius: 6px; }
.spinner {
  display: inline-block; width: 14px; height: 14px; vertical-align: -2px;
  border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.btn-stack { display: flex; flex-direction: column; gap: 8px; }
.btn.buy { background: var(--danger); color: #fff; }
.pkg-head h3 .pill { margin-left: 4px; vertical-align: 1px; }

/* =====================================================================
   เมนู: ไอคอน + ชื่อ (ชื่อสั้น .ns ใช้บนมือถือ)
   ===================================================================== */
.topbar nav a { display: inline-flex; align-items: center; gap: 6px; }
.ni { font-size: 1rem; line-height: 1; }
.ns { display: none; }
.ibo-switch { display: none; }
.ibo-quick { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.ibo-quick .btn { flex: 1; min-width: 0; justify-content: flex-start; }
.ibo-quick code { font-family: ui-monospace, Menlo, monospace; opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* =====================================================================
   มือถือ (กว้างไม่เกิน 720px)
   ===================================================================== */
@media (max-width: 720px) {
  :root { --nav-h: 62px; }
  body { font-size: 15px; -webkit-text-size-adjust: 100%; }
  .wrap { padding: 12px 12px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); }
  .card { padding: 14px; border-radius: 14px; }
  h2 { font-size: 1.2rem; }

  /* แถบบน: เหลือแค่ชื่อแอป + ปุ่มธีม + ออก */
  .topbar {
    flex-wrap: nowrap; gap: 8px; padding: 8px 12px;
    padding-top: calc(8px + env(safe-area-inset-top));
  }
  .brand { flex: 1; font-size: .98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .uname { display: none; }
  .who { gap: 6px; }
  .nl { display: none; }
  .ns { display: inline; }

  /* เมนูหลักย้ายไปเป็นแถบด้านล่างแบบแอป */
  .topbar nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 0;
    background: var(--surface); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, .06);
  }
  .topbar nav a {
    flex-direction: column; justify-content: center; gap: 2px;
    height: var(--nav-h); padding: 4px 2px; border-radius: 0;
    font-size: .72rem; color: var(--muted);
  }
  .topbar nav a .ni { font-size: 1.35rem; }
  .topbar nav a.on { background: transparent; color: var(--primary); font-weight: 600; }
  .topbar nav a.on .ni { transform: scale(1.1); }
  .topbar nav a:hover { background: transparent; }

  /* ช่องกรอก 16px กัน iPhone ซูมเอง + ปุ่มใหญ่กดง่าย */
  input, select, textarea { font-size: 16px; padding: 11px 12px; }
  textarea { font-size: 15px; }
  .btn { min-height: 44px; padding: 10px 16px; }
  .btn.sm { min-height: 40px; padding: 8px 12px; font-size: .9rem; }
  .theme-toggle { width: 40px; height: 40px; }

  /* หน้าคัดลอก */
  .tabs { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
  .tab { padding: 12px 8px; font-size: .95rem; }
  .pkg-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .items { max-height: none; overflow: visible; }
  .item { padding: 12px 2px; align-items: center; }
  .item-actions .js-take { min-width: 96px; }

  /* ป้อปอัพเลื่อนขึ้นจากด้านล่าง */
  .modal { align-items: flex-end; padding: 0; }
  .modal-box {
    max-width: none; border-radius: 18px 18px 0 0; max-height: 92vh; overflow-y: auto;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
    animation: sheet-up .2s ease-out;
  }
  .modal-foot { position: sticky; bottom: 0; }
  .modal-foot .btn { flex: 1; min-height: 50px; font-size: 1rem; }
  .x { width: 44px; height: 44px; }

  .toast { bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); text-align: center; }

  /* หลังบ้าน: ตาราง → การ์ด */
  .filters label { min-width: calc(50% - 4px); }
  .rtable thead { display: none; }
  .rtable, .rtable tbody, .rtable tr, .rtable td { display: block; width: 100%; }
  .rtable tr {
    border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px;
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px 12px;
  }
  .rtable td { border: 0; padding: 0; max-width: none; }
  .rtable td[data-label]::before {
    content: attr(data-label); display: block; font-size: .72rem; color: var(--muted); font-weight: 500;
  }
  .rtable td.rt-title { font-weight: 600; }
  .rtable td.rt-title::before { display: none; }
  .rtable td.rt-full, .rtable td.rt-actions { grid-column: 1 / -1; }
  .rtable td.rt-actions .btn { width: 100%; }
  .rtable td.rt-actions form { display: block; }
  .rtable td.content { font-size: .85rem; background: var(--bg); padding: 8px; border-radius: 8px; }
  .rtable td.content::before { margin-bottom: 4px; }
  .rtable td[colspan] { grid-column: 1 / -1; }

  /* IBO: สลับดูทีละส่วน */
  .ibo-switch {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 4px; padding: 4px; margin-bottom: 12px;
    background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
    position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 9;
  }
  .ibo-switch button {
    font: inherit; font-weight: 600; border: 0; border-radius: 9px; padding: 10px 6px; min-height: 44px;
    background: transparent; color: var(--muted); cursor: pointer;
  }
  .ibo-switch button.on { background: var(--primary); color: #fff; }
  .ibo-layout[data-view="form"] .ibo-main { display: none; }
  .ibo-layout[data-view="ibo"] .ibo-side { display: none; }
  .ibo-main .embed-frame {
    height: calc(100dvh - 58px - 72px - 110px - var(--nav-h) - env(safe-area-inset-bottom));
    min-height: 420px;
  }
  .dev-list { max-height: none; }
  .copy-row code { flex: 1; min-width: 0; }
  .ocr-drop { padding: 18px 12px; }

  .login-box { margin-top: 6vh; }
}

@keyframes sheet-up { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }

/* ---------- IBO เต็มจอ ---------- */
body.no-scroll { overflow: hidden; }
.ibo-main.full {
  position: fixed; inset: 0; z-index: 100; display: flex !important; flex-direction: column;
  background: var(--bg);
  padding: calc(8px + env(safe-area-inset-top)) 8px calc(8px + env(safe-area-inset-bottom));
}
.ibo-main.full .embed-title { display: none; }
.ibo-main.full .embed-bar { margin-bottom: 6px; }
.ibo-main.full .ibo-quick { margin-bottom: 6px; }
.ibo-main.full .embed-frame { flex: 1; height: auto !important; min-height: 0 !important; border-radius: 10px; }

@media (max-width: 720px) {
  /* แถบเหนือหน้า IBO ให้เหลือบรรทัดเดียว กรอบ IBO จะได้สูงขึ้น */
  .embed-title { display: none; }
  .embed-bar { margin-bottom: 6px; }
  .embed-bar > span { width: 100%; }
  .embed-bar > span > * { flex: 1; }
  .embed-bar #iboReload { flex: 0 0 48px; }
  .ibo-switch { margin-bottom: 8px; }
  .ibo-main .embed-frame {
    height: calc(100dvh - 58px - 60px - 52px - var(--nav-h) - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    min-height: 360px;
  }
}

/* ---------- กันหน้าเลื่อนซ้ายขวา ----------
   ลูกของ grid/flex ปกติจะไม่ยอมแคบกว่าข้อความที่ยาวที่สุด → ให้หดได้ */
.pkg, .card, .item, .item-info, .ibo-side, .ibo-main, .grid-2 > *, .ibo-layout > * { min-width: 0; }
html, body { max-width: 100%; overflow-x: clip; }

/* ---------- Activate IBO ---------- */
.cred-label { flex: 0 0 40px; font-size: .85rem; }
.copy-row code.cred { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.act-page .ibo-quick .btn { flex: 0 1 auto; }
.act-page .ibo-quick #actLastMac { flex: 1 1 auto; font-family: ui-monospace, Menlo, monospace; }
@media (max-width: 720px) {
  .topbar nav a { font-size: .66rem; }
  .act-page .ibo-quick .btn { flex: 1 1 auto; }
}

/* ---------- เว็บแผงควบคุมภายนอก (หน้าจัดการลิงก์) ---------- */
.panel-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); margin-bottom: 16px; }
.panel-card { display: flex; flex-direction: column; gap: 4px; }
.panel-card .pkg-head { flex-wrap: wrap; gap: 4px 10px; }
.panel-url { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.panel-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.panel-actions .btn { flex: 1; text-decoration: none; }
.panel-embed { margin-bottom: 16px; }
.panel-embed .embed-frame { height: 70vh; min-height: 420px; }
@media (max-width: 720px) {
  .panel-embed .embed-frame { height: calc(100dvh - 58px - 110px - var(--nav-h) - env(safe-area-inset-bottom)); min-height: 360px; }
}

/* ---------- ปุ่มสลับ 3 ปุ่ม (หน้าจัดการลิงก์) ---------- */
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px;
  padding: 4px; margin-bottom: 14px; max-width: 640px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.seg button {
  font: inherit; font-weight: 600; border: 0; border-radius: 9px; padding: 9px 8px; min-height: 42px;
  background: transparent; color: var(--muted); cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--primary); color: #fff; }
.sub-panel .panel-card { max-width: 640px; margin-bottom: 14px; }
@media (max-width: 720px) {
  .seg { max-width: none; }
  .seg button { font-size: .88rem; padding: 9px 4px; }
}

/* เมนูล่างมือถือมี 7 ปุ่ม (Owner) → ย่อให้พอดี */
@media (max-width: 720px) {
  .topbar nav a { font-size: .62rem; padding: 4px 0; }
  .topbar nav a .ni { font-size: 1.25rem; }
  .seg { max-width: 420px; }
}

/* หน้าต้นทาง: การ์ดคู่กัน (มือถือเรียงลง) */
.panel-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); }
.panel-grid .panel-card { margin-bottom: 0; }
