:root {
  --bg: #07080f;
  --bg-2: #0d0f1c;
  --surface: rgba(255, 255, 255, 0.045);
  --surface-2: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #eef0ff;
  --muted: #9aa0c3;
  --violet: hsl(258, 90%, 68%);
  --indigo: hsl(232, 92%, 66%);
  --cyan: hsl(188, 88%, 56%);
  --mint: hsl(158, 72%, 52%);
  --amber: hsl(38, 95%, 60%);
  --rose: hsl(350, 88%, 64%);
  --grad: linear-gradient(120deg, var(--violet), var(--indigo) 45%, var(--cyan));
  --radius: 20px;
  --shadow: 0 30px 80px -30px rgba(0, 0, 0, 0.7), 0 0 0 1px var(--border);
  --font: "IBM Plex Sans Thai", "Outfit", system-ui, sans-serif;
  --font-display: "Outfit", "IBM Plex Sans Thai", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- animated background ---------- */
.bg { position: fixed; inset: 0; z-index: -1; overflow: hidden; background: radial-gradient(1200px 600px at 50% -10%, #1a1640 0%, var(--bg) 60%); }
.blob { position: absolute; border-radius: 50%; filter: blur(90px); opacity: .55; animation: float 22s ease-in-out infinite; }
.b1 { width: 520px; height: 520px; background: var(--violet); top: -160px; left: -120px; }
.b2 { width: 460px; height: 460px; background: var(--cyan); top: 30%; right: -180px; opacity: .35; animation-delay: -7s; }
.b3 { width: 420px; height: 420px; background: var(--indigo); bottom: -200px; left: 30%; opacity: .4; animation-delay: -14s; }
.grain { position: absolute; inset: 0; opacity: .06; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"); }
@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(60px, 40px) scale(1.08); }
  66% { transform: translate(-40px, 70px) scale(.95); }
}

/* ---------- layout ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; max-width: 1080px; margin: 0 auto; padding: 22px 24px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-family: var(--font-display); font-size: 1.2rem; letter-spacing: .2px; }
.brand b { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.brand-mark { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 11px; background: var(--grad); color: #fff; box-shadow: 0 8px 24px -6px var(--violet); }

.ai-badge { display: flex; align-items: center; gap: 8px; padding: 7px 14px; border-radius: 999px; font-size: .82rem; background: var(--surface); border: 1px solid var(--border); backdrop-filter: blur(12px); }
.ai-badge .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.ai-badge[data-state="on"] .dot { background: var(--mint); box-shadow: 0 0 0 4px rgba(52, 211, 153, .18); animation: pulse 2s infinite; }
.ai-badge[data-state="off"] .dot { background: var(--amber); }
@keyframes pulse { 50% { box-shadow: 0 0 0 7px rgba(52, 211, 153, 0); } }

.container { max-width: 880px; margin: 0 auto; padding: 0 24px 40px; }
.hero { text-align: center; padding: 40px 0 34px; animation: rise .7s ease both; }
.eyebrow { display: inline-block; margin: 0 0 18px; padding: 6px 14px; border-radius: 999px; font-size: .78rem; letter-spacing: .6px; text-transform: uppercase; color: #c9c4ff; background: rgba(139, 92, 246, .12); border: 1px solid rgba(139, 92, 246, .3); }
h1 { font-family: var(--font-display); font-size: clamp(2rem, 5vw, 3.3rem); line-height: 1.22; margin: 0 0 16px; font-weight: 700; letter-spacing: -.5px; }
.grad { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.grad.alt { background: linear-gradient(120deg, var(--cyan), var(--mint)); -webkit-background-clip: text; background-clip: text; }
.lead { color: var(--muted); font-size: 1.08rem; max-width: 620px; margin: 0 auto; }

.glass { background: var(--surface); border: 1px solid var(--border); backdrop-filter: blur(18px) saturate(140%); -webkit-backdrop-filter: blur(18px) saturate(140%); }
.card { border-radius: var(--radius); padding: 26px; box-shadow: var(--shadow); animation: rise .6s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* ---------- dropzone ---------- */
.dropzone { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 46px 20px; border-radius: 16px; border: 1.5px dashed var(--border-strong); cursor: pointer; transition: border-color .25s, background .25s, transform .25s; position: relative; overflow: hidden; }
.dropzone::before { content: ""; position: absolute; inset: 0; background: radial-gradient(400px 160px at 50% 0%, rgba(139, 92, 246, .16), transparent 70%); opacity: 0; transition: opacity .3s; }
.dropzone:hover::before, .dropzone.drag::before { opacity: 1; }
.dropzone:hover, .dropzone.drag { border-color: var(--violet); }
.dropzone.drag { transform: scale(1.01); background: rgba(139, 92, 246, .06); }
.dz-icon { display: grid; place-items: center; width: 70px; height: 70px; border-radius: 20px; background: var(--grad); color: #fff; margin-bottom: 16px; box-shadow: 0 14px 34px -10px var(--violet); transition: transform .3s; }
.dropzone:hover .dz-icon { transform: translateY(-4px) rotate(-4deg); }
.dz-title { margin: 0; font-size: 1.2rem; font-weight: 600; }
.dz-sub { margin: 6px 0 0; color: var(--muted); font-size: .92rem; }
.link { color: #b9b2ff; text-decoration: underline; text-underline-offset: 3px; }

/* ---------- file panel ---------- */
.file-row { display: flex; align-items: center; gap: 14px; padding: 14px; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--border); }
.file-icon { flex: none; display: grid; place-items: center; width: 46px; height: 54px; border-radius: 9px; font: 700 .72rem var(--font-display); letter-spacing: .5px; color: #fff; background: linear-gradient(160deg, var(--rose), hsl(14, 90%, 60%)); }
.file-meta { flex: 1; min-width: 0; }
.file-name { margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-size { margin: 0; color: var(--muted); font-size: .85rem; }
.icon-btn { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); font-size: 1.1rem; transition: background .2s, border-color .2s; }
.icon-btn:hover { background: var(--surface-2); border-color: var(--border-strong); }

.options { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 22px 0; }
.field { display: flex; flex-direction: column; gap: 7px; }
.field.full { grid-column: 1 / -1; }
.field label, .field .label { font-size: .85rem; color: var(--muted); }
.field input { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: rgba(0, 0, 0, .25); color: var(--text); font: inherit; outline: none; transition: border-color .2s, box-shadow .2s; }
.field input:focus { border-color: var(--violet); box-shadow: 0 0 0 4px rgba(139, 92, 246, .18); }
.field input::placeholder { color: #666c90; }

.segmented { display: inline-flex; padding: 4px; border-radius: 12px; background: rgba(0, 0, 0, .25); border: 1px solid var(--border); width: fit-content; }
.segmented button { border: 0; background: transparent; padding: 8px 18px; border-radius: 9px; color: var(--muted); transition: all .2s; }
.segmented button[aria-checked="true"] { background: var(--surface-2); color: var(--text); box-shadow: 0 0 0 1px var(--border-strong); }

.toggle { display: flex; align-items: center; gap: 14px; padding: 14px; border-radius: 14px; border: 1px solid var(--border); background: rgba(0, 0, 0, .18); cursor: pointer; transition: border-color .2s; }
.toggle:hover { border-color: var(--border-strong); }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch { flex: none; width: 44px; height: 26px; border-radius: 99px; background: #2a2d45; position: relative; transition: background .25s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .25s cubic-bezier(.5, 1.6, .5, 1); }
.toggle input:checked + .switch { background: var(--grad); }
.toggle input:checked + .switch::after { transform: translateX(18px); }
.toggle input:disabled + .switch { opacity: .4; }
.toggle-text { display: flex; flex-direction: column; font-size: .92rem; }
.toggle-text small { color: var(--muted); font-size: .8rem; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 10px; padding: 11px 20px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); text-decoration: none; color: var(--text); font-weight: 600; transition: transform .2s, box-shadow .25s, background .2s; }
.btn:hover { transform: translateY(-1px); }
.btn.primary { border: 0; background: var(--grad); color: #fff; box-shadow: 0 12px 30px -10px var(--violet); background-size: 160% 100%; }
.btn.primary:hover { box-shadow: 0 16px 38px -10px var(--violet); background-position: 100% 0; }
.btn.primary:disabled { opacity: .6; cursor: wait; transform: none; }
.btn.big { width: 100%; padding: 15px 22px; font-size: 1.04rem; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 7px 14px; font-size: .85rem; }
.btn svg { transition: transform .25s; }
.btn:hover svg { transform: translateX(3px); }

.form-error, .error-inline { margin: 14px 0 0; padding: 10px 14px; border-radius: 10px; background: rgba(244, 63, 94, .1); border: 1px solid rgba(244, 63, 94, .35); color: #ffb3c0; font-size: .9rem; }

/* ---------- progress ---------- */
#progress-card { position: relative; }
.progress-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; }
.progress-head h2 { margin: 2px 0 0; font-size: 1.35rem; font-weight: 600; }
.percent { font: 700 2.2rem var(--font-display); background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: .85rem; margin: 0; }

.stepper { list-style: none; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 0; margin: 22px 0 16px; counter-reset: s; }
.stepper li { display: flex; align-items: center; gap: 8px; font-size: .82rem; color: var(--muted); padding: 8px 10px; border-radius: 10px; background: rgba(0, 0, 0, .18); border: 1px solid var(--border); transition: all .3s; }
.s-dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: #3a3e5c; transition: all .3s; }
.stepper li.active { color: var(--text); border-color: rgba(139, 92, 246, .5); background: rgba(139, 92, 246, .1); }
.stepper li.active .s-dot { background: var(--violet); box-shadow: 0 0 0 4px rgba(139, 92, 246, .25); animation: pulse-v 1.4s infinite; }
.stepper li.done { color: #c8ffe8; }
.stepper li.done .s-dot { background: var(--mint); }
@keyframes pulse-v { 50% { box-shadow: 0 0 0 7px rgba(139, 92, 246, 0); } }

.bar { height: 10px; border-radius: 99px; background: rgba(0, 0, 0, .3); overflow: hidden; border: 1px solid var(--border); }
.bar-fill { height: 100%; width: 0; background: var(--grad); border-radius: inherit; transition: width .5s ease; position: relative; }
.bar-fill::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .35), transparent); animation: shine 1.6s infinite; }
@keyframes shine { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
#job-sub { margin-top: 10px; }

.pages-head { display: flex; justify-content: space-between; align-items: center; margin: 26px 0 12px; flex-wrap: wrap; gap: 10px; }
.pages-head h3 { margin: 0; font-size: 1rem; }
.legend { display: flex; gap: 14px; font-size: .78rem; color: var(--muted); }
.legend span { display: flex; align-items: center; gap: 6px; }
.lg { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.lg.ai, .tile.ai { --c: var(--mint); }
.lg.text, .tile.text { --c: var(--amber); }
.lg.failed, .tile.failed { --c: var(--rose); }
.lg { background: var(--c); }

.page-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 7px; max-height: 260px; overflow-y: auto; padding: 2px; }
.tile { aspect-ratio: 3 / 4; border-radius: 7px; border: 1px solid var(--border); background: rgba(255, 255, 255, .04); display: grid; place-items: center; font: 600 .72rem var(--font-display); color: var(--muted); position: relative; overflow: hidden; transition: transform .2s, border-color .2s, background .3s; }
.tile.pending::after { content: ""; position: absolute; inset: 0; background: linear-gradient(110deg, transparent 30%, rgba(255, 255, 255, .08), transparent 70%); animation: shine 1.8s infinite; }
.tile.ai, .tile.text, .tile.failed { color: #0b0d18; background: var(--c); border-color: transparent; cursor: pointer; animation: pop .35s cubic-bezier(.5, 1.8, .5, 1); }
.tile.ai:hover, .tile.text:hover, .tile.failed:hover { transform: translateY(-2px) scale(1.06); box-shadow: 0 8px 18px -6px var(--c); }
@keyframes pop { from { transform: scale(.6); opacity: .3; } }
.hint { margin-top: 10px; }

.result { text-align: center; margin-top: 26px; padding-top: 24px; border-top: 1px solid var(--border); animation: rise .5s ease both; }
.result-icon { display: grid; place-items: center; width: 64px; height: 64px; margin: 0 auto 16px; border-radius: 50%; background: linear-gradient(140deg, var(--mint), var(--cyan)); color: #062018; box-shadow: 0 14px 34px -10px var(--mint); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 20px; }
.stat { padding: 12px; border-radius: 12px; background: rgba(0, 0, 0, .2); border: 1px solid var(--border); }
.stat b { display: block; font: 700 1.3rem var(--font-display); }
.stat span { font-size: .78rem; color: var(--muted); }
.result-actions { display: grid; grid-template-columns: 2fr 1fr; gap: 10px; }
.warn { margin: 14px 0 0; font-size: .86rem; color: #ffd89a; }

.error-box { margin-top: 22px; padding: 18px; border-radius: 14px; background: rgba(244, 63, 94, .08); border: 1px solid rgba(244, 63, 94, .35); }
.error-box p { color: #ffb3c0; margin: 6px 0 14px; word-break: break-word; }
.cancel { position: absolute; top: 22px; right: 22px; }
#progress-card .progress-head { padding-right: 80px; }

/* ---------- features ---------- */
.features { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-top: 36px; }
.feature { border-radius: 18px; padding: 22px; transition: transform .25s, border-color .25s; }
.feature:hover { transform: translateY(-4px); border-color: var(--border-strong); }
.feature h3 { margin: 10px 0 6px; font-size: 1.02rem; }
.feature p { margin: 0; color: var(--muted); font-size: .9rem; }
.f-icon { font-size: 1.6rem; }

.footer { text-align: center; color: #6a7096; font-size: .82rem; padding: 10px 0 34px; }

/* ---------- modal ---------- */
.modal { width: min(1100px, 94vw); height: min(86vh, 900px); padding: 0; border-radius: 20px; border: 1px solid var(--border-strong); background: var(--bg-2); color: var(--text); box-shadow: 0 40px 120px rgba(0, 0, 0, .7); }
.modal[open] { display: flex; flex-direction: column; animation: rise .3s ease; }
.modal::backdrop { background: rgba(4, 5, 12, .7); backdrop-filter: blur(6px); }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 1.05rem; }
.modal-nav { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.chip { font-size: .75rem; padding: 3px 10px; border-radius: 99px; background: var(--c, var(--surface-2)); color: #0b0d18; font-weight: 600; }
.chip.ai { --c: var(--mint); } .chip.text { --c: var(--amber); } .chip.failed { --c: var(--rose); }
.modal-body { flex: 1; display: grid; grid-template-columns: 1fr 1fr; min-height: 0; }
.preview-img { margin: 0; padding: 16px; overflow: auto; background: #04050a; border-right: 1px solid var(--border); }
.preview-img img { width: 100%; border-radius: 6px; display: block; }
.preview-text { padding: 16px 20px; overflow: auto; }
/* ---------- user bar & auth ---------- */
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.quota-pill { font-size: .8rem; padding: 6px 12px; border-radius: 999px; background: rgba(20, 184, 166, .12); border: 1px solid rgba(20, 184, 166, .35); color: #5eead4; white-space: nowrap; }
.quota-pill b { font-weight: 600; }
.user-chip { display: flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: .86rem; }
.user-avatar { font-size: .95rem; }
.user-name { font-weight: 600; }
.role-badge { font-size: .68rem; padding: 2px 7px; border-radius: 999px; text-transform: uppercase; font-weight: 700; letter-spacing: .4px; }
.role-badge.admin { background: rgba(139, 92, 246, .22); color: #c4b5fd; border: 1px solid rgba(139, 92, 246, .4); }
.role-badge.user { background: rgba(255, 255, 255, .08); color: var(--muted); border: 1px solid var(--border); }

/* ---------- auth dialog ---------- */
.auth-dialog { width: min(440px, 92vw); height: auto; max-height: 90vh; border-radius: 24px; padding: 0; }
.auth-card { padding: 36px 30px; text-align: center; }
.auth-icon { display: grid; place-items: center; width: 62px; height: 62px; border-radius: 18px; background: var(--grad); color: #fff; margin: 0 auto 16px; box-shadow: 0 12px 30px -8px var(--violet); }
.auth-card h2 { margin: 0 0 6px; font-family: var(--font-display); font-size: 1.45rem; }
.auth-sub { margin: 0 0 22px; color: var(--muted); font-size: .88rem; line-height: 1.5; }
.password-field { position: relative; display: flex; align-items: center; }
.password-field input { width: 100%; padding-right: 42px; }
.icon-toggle-pwd { position: absolute; right: 10px; background: none; border: none; cursor: pointer; font-size: 1.05rem; opacity: .65; transition: opacity .2s; }
.icon-toggle-pwd:hover { opacity: 1; }
.auth-error { margin: 14px 0 0; padding: 10px 14px; border-radius: 10px; font-size: .84rem; background: rgba(244, 63, 94, .12); border: 1px solid rgba(244, 63, 94, .35); color: #fda4af; text-align: left; }

/* ---------- admin dialog ---------- */
.admin-dialog { width: min(920px, 94vw); height: min(84vh, 780px); }
.admin-body { padding: 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 22px; }
.admin-card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 20px; }
.admin-card h4 { margin: 0 0 14px; font-size: 1.05rem; font-family: var(--font-display); }
.admin-form-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; align-items: end; }
.styled-select { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: #131526; color: var(--text); font: inherit; outline: none; }
.styled-select:focus { border-color: var(--violet); }
.table-wrap { overflow-x: auto; }
.user-table { width: 100%; border-collapse: collapse; font-size: .88rem; text-align: left; }
.user-table th { padding: 10px 12px; border-bottom: 1px solid var(--border-strong); color: var(--muted); font-weight: 600; }
.user-table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.admin-msg { font-size: .85rem; margin-left: 12px; }
.btn.danger { background: rgba(244, 63, 94, .12); border-color: rgba(244, 63, 94, .35); color: #fda4af; }
.btn.danger:hover { background: rgba(244, 63, 94, .25); }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .options, .features, .result-actions { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .stepper { grid-template-columns: repeat(2, 1fr); }
  .modal-body { grid-template-columns: 1fr; grid-template-rows: 45% 55%; }
  .preview-img { border-right: 0; border-bottom: 1px solid var(--border); }
  .card { padding: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
