:root {
  --bg: #f6f5f2;
  --card: #ffffff;
  --ink: #1c1b19;
  --muted: #6b6860;
  --line: #e4e1da;
  --soft: #efede8;
  --accent: #0f6e64;
  --accent-ink: #ffffff;
  --accent-soft: #e1f1ee;
  --danger: #b4321f;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --r: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141412; --card: #1e1d1a; --ink: #eeece6; --muted: #a19d93; --line: #33312c;
    --soft: #262521; --accent: #4cc2b3; --accent-ink: #0d1f1c; --accent-soft: #1b3330; --danger: #ff7a66;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.4em; margin: 8px 0 0; font-size: 14px; }

/* ปุ่ม */
.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; font-weight: 500; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--ink);
}
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.block { width: 100%; justify-content: center; padding: 11px; }
.btn:disabled { opacity: .55; cursor: default; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px; }
.icon-btn { background: none; border: 0; font-size: 22px; padding: 6px 10px; cursor: pointer; border-radius: 8px; }
.icon-btn:hover { background: var(--soft); }

/* หน้าเข้าสู่ระบบ */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px 24px; box-shadow: var(--shadow);
}
.auth-logo { font-size: 40px; }
.auth-card h1 { margin: 6px 0 2px; font-size: 22px; }
.auth-card label, .form label { display: block; font-size: 14px; color: var(--muted); margin-top: 14px; }
.auth-card input, .form input, .form select {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); font-size: 16px;
}
.auth-card input:focus, .form input:focus, .search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.auth-card .btn { margin-top: 20px; }

/* แถบบน */
.top {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 600; }
.brand-ic { font-size: 22px; }
.search { flex: 1; max-width: 520px; margin-left: auto; }
.search input {
  width: 100%; padding: 9px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--card); font-size: 15px;
}
.menu {
  position: absolute; right: 12px; top: 56px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); padding: 6px; min-width: 220px; display: flex; flex-direction: column;
}
.menu a, .menu button {
  text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px;
  cursor: pointer; text-decoration: none; color: var(--ink);
}
.menu a:hover, .menu button:hover { background: var(--soft); }
.who { padding: 10px 12px; border-bottom: 1px solid var(--line); margin-bottom: 4px; font-size: 14px; }
.who b { display: block; font-size: 15px; }

main { max-width: 1200px; margin: 0 auto; padding: 16px 16px 120px; }

/* แถบเครื่องมือ + breadcrumb */
.bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.crumbs { display: flex; align-items: center; flex-wrap: wrap; gap: 2px; font-size: 17px; flex: 1; min-width: 0; }
.crumbs a { text-decoration: none; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.crumbs a:hover { background: var(--soft); color: var(--ink); }
.crumbs .sep { color: var(--muted); }
.crumbs .here { font-weight: 600; padding: 2px 6px; word-break: break-word; }
.actions { display: flex; gap: 8px; }

/* ตารางการ์ด */
.section-title { font-size: 13px; color: var(--muted); margin: 18px 0 8px; font-weight: 500; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.folders-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.fcard {
  display: flex; align-items: center; gap: 10px; padding: 12px 8px 12px 14px; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--r); cursor: pointer; position: relative; min-width: 0;
}
.fcard:hover { border-color: var(--accent); }
.fcard .ic { font-size: 26px; }
.fcard .tx { min-width: 0; flex: 1; }
.fcard .nm { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fcard .ct { font-size: 12px; color: var(--muted); }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  overflow: hidden; position: relative; display: flex; flex-direction: column; min-width: 0;
}
.card:hover { border-color: var(--accent); }
.thumb {
  aspect-ratio: 1 / 1; background: var(--soft); display: flex; align-items: center; justify-content: center;
  cursor: pointer; position: relative; overflow: hidden;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .big { font-size: 44px; }
.badge {
  position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.65); color: #fff;
  border-radius: 999px; padding: 1px 9px; font-size: 12px;
}
.meta { padding: 8px 34px 10px 10px; min-width: 0; }
.meta .nm { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta .sb { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.more {
  position: absolute; right: 4px; bottom: 8px; background: none; border: 0; font-size: 18px;
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer; color: var(--muted);
}
.fcard .more { position: static; }
.more:hover { background: var(--soft); color: var(--ink); }

.empty { text-align: center; color: var(--muted); padding: 60px 16px; border: 2px dashed var(--line); border-radius: 16px; }
.empty .big { font-size: 46px; }

/* เมนูย่อย */
.pop {
  position: fixed; z-index: 60; background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); padding: 6px; min-width: 190px; display: flex; flex-direction: column;
}
.pop button { text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 8px; cursor: pointer; }
.pop button:hover { background: var(--soft); }
.pop button.danger { color: var(--danger); }

/* หน้าต่าง (modal) */
.modal-wrap {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.45); display: flex;
  align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--card); border-radius: 16px; width: 100%; max-width: 460px; max-height: 90vh;
  overflow: auto; box-shadow: var(--shadow); padding: 20px;
}
.modal h2 { margin: 0 0 10px; font-size: 18px; }
.modal .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.check { display: flex !important; align-items: center; gap: 10px; color: var(--ink) !important; margin-top: 10px !important; cursor: pointer; }
.check input { width: 18px !important; height: 18px; margin: 0 !important; accent-color: var(--accent); }
.linkbox { display: flex; gap: 6px; margin-top: 10px; }
.linkbox input { flex: 1; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 14px; min-width: 0; }
.tree { border: 1px solid var(--line); border-radius: 10px; max-height: 320px; overflow: auto; margin-top: 8px; }
.tree button {
  display: block; width: 100%; text-align: left; background: none; border: 0; padding: 8px 12px; cursor: pointer;
  border-bottom: 1px solid var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tree button:last-child { border-bottom: 0; }
.tree button.sel { background: var(--accent-soft); font-weight: 600; }
.tree button:disabled { opacity: .4; cursor: default; }

/* ตัวดูรูป/วิดีโอ */
.viewer { position: fixed; inset: 0; z-index: 70; background: #0b0b0a; display: flex; flex-direction: column; color: #fff; }
.viewer .vtop { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.viewer .vtitle { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.viewer .vbtn { background: rgba(255,255,255,.12); border: 0; color: #fff; padding: 8px 12px; border-radius: 10px; cursor: pointer; text-decoration: none; white-space: nowrap; }
.viewer .vbtn:hover { background: rgba(255,255,255,.22); }
.viewer .stage { flex: 1; display: flex; align-items: center; justify-content: center; position: relative; min-height: 0; }
.viewer .stage img, .viewer .stage video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .nav {
  position: absolute; top: 50%; transform: translateY(-50%); background: rgba(255,255,255,.14); border: 0; color: #fff;
  width: 44px; height: 64px; font-size: 28px; border-radius: 12px; cursor: pointer;
}
.viewer .nav.l { left: 10px; } .viewer .nav.r { right: 10px; }
.viewer .vinfo { padding: 8px 14px 14px; font-size: 13px; color: #bbb; text-align: center; }

/* อัปโหลด */
.fab {
  position: fixed; right: 18px; bottom: 22px; z-index: 30; width: 58px; height: 58px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--accent-ink); font-size: 30px; box-shadow: var(--shadow); cursor: pointer;
}
.uploads {
  position: fixed; right: 16px; bottom: 16px; z-index: 40; width: min(380px, calc(100vw - 32px));
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden;
}
.uploads-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.up-list { max-height: 300px; overflow: auto; }
.uploads.min .up-list { display: none; }
.up { padding: 9px 14px; border-bottom: 1px solid var(--line); font-size: 13px; }
.up:last-child { border-bottom: 0; }
.up .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.up .st { color: var(--muted); font-size: 12px; display: flex; justify-content: space-between; gap: 8px; }
.up.err .st { color: var(--danger); }
.prog { height: 5px; background: var(--soft); border-radius: 99px; overflow: hidden; margin-top: 5px; }
.prog > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
.up.done .prog > i { background: #3a9d5d; }
.drop {
  position: fixed; inset: 0; z-index: 90; background: color-mix(in srgb, var(--accent) 22%, transparent);
  border: 4px dashed var(--accent); display: flex; align-items: center; justify-content: center; pointer-events: none;
}
.drop div { background: var(--card); padding: 18px 26px; border-radius: 14px; font-size: 18px; font-weight: 600; }

/* ตาราง (ผู้ใช้/ลิงก์) */
.list { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.row:last-child { border-bottom: 0; }
.row .main { flex: 1; min-width: 180px; }
.row .main b { display: block; word-break: break-word; }
.row .main span { font-size: 13px; color: var(--muted); }
.chips { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.chip { font-size: 12px; background: var(--soft); border-radius: 999px; padding: 1px 9px; color: var(--muted); }
.chip.on { background: var(--accent-soft); color: var(--accent); }
.chip.off { background: transparent; border: 1px solid var(--line); text-decoration: line-through; }
.stats { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 16px; min-width: 130px; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 13px; color: var(--muted); }
h1.page { font-size: 20px; margin: 4px 0 14px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 12px; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px); font-size: 14px;
}
.toast.bad { background: var(--danger); color: #fff; }

@media (max-width: 640px) {
  .top { gap: 8px; padding: 8px 10px; }
  .brand-tx { display: none; }
  main { padding: 12px 10px 120px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
  .folders-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .fcard { padding: 10px 4px 10px 10px; }
  .actions .btn .lbl { display: none; }
  .crumbs { font-size: 15px; }
  .viewer .nav { width: 36px; height: 52px; }
  .vbtn .lbl { display: none; }
}
@media (max-width: 380px) { .folders-grid { grid-template-columns: 1fr; } }
