/* ========================= ระบบหลังบ้าน — โกดังคอม ไอปลาวาฬ83 ========================= */
:root {
  --p: #0B4FD8; --p2: #18B6FF; --gold: #D9A527; --ink: #07132B;
  --bg: #F4F7FD; --card: #fff; --line: #DEE7F7; --muted: #5A6B8C; --muted2: #8496B4;
  --ok: #12a45a; --err: #E0343C;
  --sh: 0 2px 10px rgba(9,32,84,.06); --sh2: 0 16px 44px -18px rgba(9,32,84,.3);
  --f: "IBM Plex Sans Thai", -apple-system, "Segoe UI", sans-serif;
  --fd: "Space Grotesk", var(--f);
}
* { box-sizing: border-box; margin: 0; }
body { font-family: var(--f); background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.65; -webkit-font-smoothing: antialiased; }
img { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
a { color: var(--p); text-decoration: none; }
:focus-visible { outline: 2px solid var(--p2); outline-offset: 2px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 9px 16px; border-radius: 11px; font-weight: 600; font-size: 14.5px; border: 1px solid transparent; transition: .2s; white-space: nowrap; }
.btn:hover { transform: translateY(-1px); }
.btn.primary { background: linear-gradient(120deg, var(--p), var(--p2)); color: #fff; box-shadow: 0 8px 20px -10px rgba(11,79,216,.9); }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { border-color: var(--p); color: var(--p); }
.btn.danger { background: #fff; border-color: #f3c9cb; color: var(--err); }
.btn.danger:hover { background: var(--err); color: #fff; border-color: transparent; }
.btn.gold { background: linear-gradient(120deg, #B8860B, var(--gold)); color: #2b1d00; }
.btn.sm { padding: 6px 12px; font-size: 13.5px; border-radius: 9px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.icon-btn { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; font-size: 17px; color: var(--muted); }
.icon-btn:hover { background: var(--bg); color: var(--ink); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.hint { font-size: 13px; color: var(--muted); }
.only-m { display: none; }

/* ------------------------------- login ------------------------------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; background: radial-gradient(ellipse at top, #e8f0ff, #f6f9ff 60%); z-index: 50; }
.login-card { width: min(400px, 100%); background: #fff; border: 1px solid var(--line); border-radius: 22px; padding: 34px 30px; box-shadow: var(--sh2); text-align: center; }
.login-logo { width: 76px; height: 76px; margin: 0 auto 16px; border-radius: 20px; overflow: hidden; background: #04102a; box-shadow: 0 12px 30px -14px rgba(11,79,216,.8); }
.login-logo img { width: 100%; height: 100%; object-fit: contain; }
.login-card h1 { font-family: var(--fd); font-size: 24px; }
.login-card > p { color: var(--muted); margin-bottom: 22px; font-size: 14px; }
.login-msg { min-height: 1.4em; font-size: 13.5px; color: var(--err); margin-top: 10px; }
.login-hint { display: block; margin-top: 14px; font-size: 12px; color: var(--muted2); line-height: 1.6; }
.login-hint code { background: var(--bg); padding: 1px 5px; border-radius: 5px; }

/* --------------------------------- app ------------------------------- */
.app { display: grid; grid-template-columns: 244px 1fr; min-height: 100vh; }
.side { background: linear-gradient(175deg, var(--ink), #061a3d); color: rgba(255,255,255,.8); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.side-brand { display: flex; align-items: center; gap: 11px; padding: 18px 18px 16px; border-bottom: 1px solid rgba(255,255,255,.1); }
.side-brand img { width: 40px; height: 40px; border-radius: 11px; object-fit: contain; background: #04102a; flex: none; }
.side-brand b { display: block; color: #fff; font-size: 15px; }
.side-brand i { font-style: normal; font-size: 11.5px; color: rgba(255,255,255,.55); }
.side-nav { flex: 1; overflow-y: auto; padding: 12px 10px; display: grid; gap: 2px; align-content: start; }
.side-nav .grp { font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: rgba(255,255,255,.4); padding: 12px 12px 5px; font-family: var(--fd); }
.side-nav button { display: flex; align-items: center; gap: 9px; width: 100%; text-align: left; padding: 9px 12px; border-radius: 10px; font-size: 14.3px; color: rgba(255,255,255,.8); transition: .2s; }
.side-nav button:hover { background: rgba(255,255,255,.08); color: #fff; }
.side-nav button.on { background: linear-gradient(120deg, var(--p), var(--p2)); color: #fff; box-shadow: 0 8px 20px -12px rgba(24,182,255,.9); }
.side-nav .badge { margin-left: auto; background: rgba(255,255,255,.16); border-radius: 99px; font-size: 11px; padding: 1px 8px; }
.side-foot { padding: 12px; display: grid; gap: 7px; border-top: 1px solid rgba(255,255,255,.1); }
.side-foot .btn.ghost { background: rgba(255,255,255,.06); border-color: rgba(255,255,255,.14); color: rgba(255,255,255,.85); }
.side-foot .btn.ghost:hover { background: rgba(255,255,255,.14); color: #fff; }

.main { display: flex; flex-direction: column; min-width: 0; }
.bar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 14px; padding: 11px 20px; background: rgba(255,255,255,.9); backdrop-filter: blur(14px); border-bottom: 1px solid var(--line); }
.bar-title b { display: block; font-family: var(--fd); font-size: 18px; letter-spacing: -.01em; }
.bar-title i { font-style: normal; font-size: 12.5px; color: var(--muted); }
.bar-right { margin-left: auto; display: flex; align-items: center; gap: 9px; }
.saved { font-size: 12px; color: var(--muted2); }
.kv-state { font-size: 11.5px; padding: 3px 10px; border-radius: 99px; font-weight: 600; }
.kv-state.ok { background: rgba(18,164,90,.12); color: var(--ok); }
.kv-state.no { background: rgba(224,52,60,.1); color: var(--err); }

.work { display: grid; grid-template-columns: 1fr; flex: 1; min-height: 0; }
.work.split { grid-template-columns: minmax(0,1fr) minmax(360px, 42%); }
.pane { padding: 22px; overflow-y: auto; }
.preview[hidden] { display: none !important; }
.preview { display: none; border-left: 1px solid var(--line); background: #eaf0fb; flex-direction: column; min-width: 0; }
.work.split .preview { display: flex; }
.preview-bar { display: flex; align-items: center; gap: 9px; padding: 9px 12px; background: #fff; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.pv-tabs { display: flex; gap: 3px; background: var(--bg); padding: 3px; border-radius: 10px; }
.pv-tabs button { padding: 5px 12px; border-radius: 8px; font-size: 13px; font-weight: 500; }
.pv-tabs button.on { background: #fff; box-shadow: var(--sh); color: var(--p); }
.preview select { padding: 6px 10px; border: 1px solid var(--line); border-radius: 9px; font-size: 13.5px; background: #fff; }
.preview-stage { flex: 1; overflow: auto; display: grid; justify-items: center; align-items: stretch; padding: 16px; }
.preview-stage iframe { width: 100%; height: 100%; min-height: 700px; border: 0; background: #fff; border-radius: 12px; box-shadow: var(--sh2); transition: width .3s; }
.preview-stage.tab iframe { width: 820px; max-width: 100%; }
.preview-stage.mob iframe { width: 400px; max-width: 100%; }

/* -------------------------------- cards ------------------------------ */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 20px; box-shadow: var(--sh); margin-bottom: 16px; }
.card > h3 { font-size: 16.5px; margin-bottom: 4px; display: flex; align-items: center; gap: 8px; }
.card > .hint { margin-bottom: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.fld { display: grid; gap: 5px; margin-bottom: 13px; }
.fld > span { font-size: 13px; font-weight: 600; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.fld .tip { font-weight: 400; font-size: 11.5px; color: var(--muted2); }
.inp, .fld input, .fld select, .fld textarea {
  width: 100%; padding: 10px 13px; border: 1.5px solid var(--line); border-radius: 11px;
  background: #fff; font-size: 14.5px; transition: .2s;
}
.fld textarea { resize: vertical; min-height: 78px; line-height: 1.6; }
.inp:focus, .fld input:focus, .fld select:focus, .fld textarea:focus { outline: none; border-color: var(--p); box-shadow: 0 0 0 3.5px rgba(11,79,216,.11); }
.chk { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; user-select: none; }
.chk input { width: 17px; height: 17px; accent-color: var(--p); }
.color-row { display: flex; align-items: center; gap: 8px; }
.color-row input[type=color] { width: 46px; height: 40px; padding: 2px; border-radius: 10px; border: 1.5px solid var(--line); background: #fff; cursor: pointer; }

/* -------------------------------- list ------------------------------- */
.items { display: grid; gap: 12px; }
.item { border: 1px solid var(--line); border-radius: 14px; background: #fff; overflow: hidden; transition: border-color .2s, box-shadow .2s; }
.item.open { border-color: rgba(11,79,216,.4); box-shadow: var(--sh2); }
.item-head { display: flex; align-items: center; gap: 12px; padding: 11px 14px; cursor: pointer; }
.item-head:hover { background: var(--bg); }
.item-thumb { width: 46px; height: 46px; border-radius: 10px; object-fit: cover; background: var(--bg); flex: none; border: 1px solid var(--line); }
.item-thumb.empty { display: grid; place-items: center; color: var(--muted2); font-size: 17px; }
.item-txt { flex: 1; min-width: 0; }
.item-txt b { display: block; font-size: 14.8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-txt i { font-style: normal; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.item-tools { display: flex; gap: 4px; align-items: center; }
.item-tools button { width: 30px; height: 30px; border-radius: 8px; color: var(--muted); font-size: 14px; }
.item-tools button:hover { background: var(--bg); color: var(--p); }
.item-tools button.del:hover { background: rgba(224,52,60,.1); color: var(--err); }
.item-body { padding: 0 14px 16px; border-top: 1px solid var(--line); padding-top: 14px; }
.pill { font-size: 11.5px; padding: 2px 9px; border-radius: 99px; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.pill.on { background: rgba(18,164,90,.12); color: var(--ok); border-color: transparent; }
.pill.off { background: rgba(224,52,60,.1); color: var(--err); border-color: transparent; }
.pill.gold { background: linear-gradient(120deg,#fdf3d8,#fbe9b8); color: #6b4d00; border-color: rgba(217,165,39,.35); }

/* ------------------------------ image field --------------------------- */
.imgf { display: flex; gap: 12px; align-items: flex-start; padding: 11px; border: 1.5px dashed var(--line); border-radius: 13px; background: var(--bg); }
.imgf-prev { width: 92px; border-radius: 10px; overflow: hidden; background: #fff; border: 1px solid var(--line); flex: none; display: grid; place-items: center; min-height: 92px; }
.imgf-prev img, .imgf-prev video { width: 100%; height: auto; display: block; }
.imgf-prev.empty::after { content: 'ไม่มีรูป'; font-size: 11.5px; color: var(--muted2); }
.imgf-info { flex: 1; min-width: 0; display: grid; gap: 7px; }
.imgf-info .path { font-size: 12px; color: var(--muted); word-break: break-all; background: #fff; padding: 5px 9px; border-radius: 8px; border: 1px solid var(--line); }
.imgf-info .dim { font-size: 11.5px; color: var(--muted2); }

/* ------------------------------- sublist ------------------------------ */
.sub { display: grid; gap: 8px; }
.sub-row { display: flex; gap: 8px; align-items: center; }
.sub-row .inp { flex: 1; }
.sub-row button { width: 34px; height: 34px; border-radius: 9px; color: var(--muted); flex: none; }
.sub-row button:hover { background: rgba(224,52,60,.1); color: var(--err); }

/* -------------------------------- leads ------------------------------- */
.lead { border: 1px solid var(--line); border-radius: 13px; background: #fff; padding: 14px 16px; display: grid; gap: 6px; }
.lead.unread { border-color: rgba(11,79,216,.4); background: linear-gradient(120deg, rgba(11,79,216,.03), #fff); }
.lead-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.lead-top b { font-size: 15.5px; }
.lead-meta { font-size: 12.5px; color: var(--muted2); margin-left: auto; }
.lead-msg { font-size: 14px; color: var(--muted); background: var(--bg); padding: 9px 12px; border-radius: 10px; }

/* -------------------------------- modal ------------------------------- */
.modal[hidden] { display: none !important; }
.modal { position: fixed; inset: 0; z-index: 60; background: rgba(7,19,43,.55); backdrop-filter: blur(4px); display: grid; place-items: center; padding: 18px; }
.modal-card { width: min(980px, 100%); max-height: 92vh; background: #fff; border-radius: 20px; box-shadow: var(--sh2); display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 15px 20px; border-bottom: 1px solid var(--line); }
.modal-head b { font-size: 16.5px; }
.modal-tabs { display: flex; gap: 4px; padding: 10px 20px 0; }
.modal-tabs button { padding: 8px 15px; border-radius: 10px 10px 0 0; font-size: 14px; font-weight: 500; color: var(--muted); border-bottom: 2px solid transparent; }
.modal-tabs button.on { color: var(--p); border-bottom-color: var(--p); background: var(--bg); }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; }
.lib { display: grid; grid-template-columns: repeat(auto-fill, minmax(122px, 1fr)); gap: 10px; margin-top: 12px; }
.lib figure { border: 2px solid var(--line); border-radius: 12px; overflow: hidden; cursor: pointer; background: #fff; transition: .2s; }
.lib figure:hover { border-color: var(--p); transform: translateY(-3px); box-shadow: var(--sh2); }
.lib img { width: 100%; aspect-ratio: 1; object-fit: cover; }
.lib figcaption { font-size: 10.5px; padding: 4px 6px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-groups { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.lib-groups button { padding: 5px 13px; border-radius: 99px; border: 1px solid var(--line); font-size: 13px; }
.lib-groups button.on { background: linear-gradient(120deg, var(--p), var(--p2)); color: #fff; border-color: transparent; }

.up-drop { border: 2px dashed var(--line); border-radius: 15px; padding: 34px 20px; text-align: center; cursor: pointer; transition: .2s; display: grid; gap: 5px; }
.up-drop:hover, .up-drop.over { border-color: var(--p); background: rgba(11,79,216,.04); }
.up-drop b { font-size: 15.5px; }
.up-drop i { font-style: normal; font-size: 13px; color: var(--muted); }

.cropper { margin-top: 14px; }
.crop-tools { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.ratios { display: flex; gap: 4px; flex-wrap: wrap; }
.ratios button { padding: 5px 12px; border-radius: 9px; border: 1px solid var(--line); font-size: 13px; }
.ratios button.on { background: var(--p); color: #fff; border-color: transparent; }
.crop-stage { position: relative; display: inline-block; max-width: 100%; background: #0d1a34; border-radius: 12px; overflow: hidden; line-height: 0; }
.crop-stage img { max-width: 100%; max-height: 52vh; width: auto; user-select: none; -webkit-user-drag: none; }
.crop-box { position: absolute; border: 2px solid var(--p2); box-shadow: 0 0 0 9999px rgba(7,19,43,.5); cursor: move; }
.crop-box .ch { position: absolute; width: 15px; height: 15px; background: #fff; border: 2px solid var(--p2); border-radius: 4px; }
.crop-box .ch[data-h=nw] { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-box .ch[data-h=ne] { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-box .ch[data-h=sw] { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-box .ch[data-h=se] { bottom: -8px; right: -8px; cursor: nwse-resize; }
.crop-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; flex-wrap: wrap; }
.url-prev { margin-top: 14px; }
.url-prev img { max-width: 260px; border-radius: 12px; border: 1px solid var(--line); }

/* -------------------------------- toast ------------------------------- */
.toast { position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 20px); z-index: 90; background: var(--ink); color: #fff; padding: 12px 22px; border-radius: 13px; font-size: 14.5px; box-shadow: var(--sh2); opacity: 0; pointer-events: none; transition: .3s; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { background: linear-gradient(120deg, #0e7a45, var(--ok)); }
.toast.err { background: linear-gradient(120deg, #a8232a, var(--err)); }

/* ------------------------------ dashboard ----------------------------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 13px; margin-bottom: 18px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 15px; padding: 17px 19px; box-shadow: var(--sh); }
.stat b { display: block; font-family: var(--fd); font-size: 28px; letter-spacing: -.02em; background: linear-gradient(120deg, var(--p), var(--p2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.stat i { font-style: normal; font-size: 13px; color: var(--muted); }
.quick { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 11px; }
.quick button { background: #fff; border: 1px solid var(--line); border-radius: 13px; padding: 15px; text-align: left; box-shadow: var(--sh); transition: .2s; }
.quick button:hover { border-color: var(--p); transform: translateY(-3px); box-shadow: var(--sh2); }
.quick b { display: block; font-size: 14.8px; }
.quick i { font-style: normal; font-size: 12.5px; color: var(--muted); }
.guide { display: grid; gap: 9px; counter-reset: g; }
.guide li { display: flex; gap: 11px; font-size: 14.3px; }
.guide li::before { counter-increment: g; content: counter(g); width: 25px; height: 25px; border-radius: 8px; background: linear-gradient(120deg, var(--p), var(--p2)); color: #fff; display: grid; place-items: center; font-size: 12.5px; font-weight: 700; flex: none; }
.guide code { background: var(--bg); padding: 1px 6px; border-radius: 6px; font-size: 12.5px; }

/* ------------------------------ responsive ---------------------------- */
@media (max-width: 1180px) { .work.split { grid-template-columns: 1fr; } .work.split .preview { display: none; } }
@media (max-width: 1000px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; left: 0; top: 0; width: 250px; z-index: 40; transform: translateX(-100%); transition: transform .3s; }
  .side.open { transform: none; }
  .only-m { display: grid; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid3 { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .pane { padding: 14px; }
  .bar { padding: 10px 12px; flex-wrap: wrap; }
  .bar-right { width: 100%; }
  .stats { grid-template-columns: 1fr 1fr; }
  .modal-card { max-height: 96vh; }
}
