:root {
  --deep-1: #04121f;
  --deep-2: #0a2540;
  --deep-3: #0e3a5c;
  --cyan: #5ed0ff;
  --foam: #dff1fb;
  --ink: #0a2540;
  --panel: rgba(9, 30, 52, 0.55);
  --panel-brd: rgba(120, 190, 240, 0.22);
  --radius: 16px;
  --err: #ff8ba0;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }

body {
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
  color: var(--foam);
  background: var(--deep-1);
  -webkit-font-smoothing: antialiased;
}

/* ══════ ฉากทะเล ══════ */
.ocean {
  position: relative;
  min-height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% -10%, #14507e 0%, transparent 55%),
    linear-gradient(180deg, var(--deep-3) 0%, var(--deep-2) 42%, var(--deep-1) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding:
    calc(env(safe-area-inset-top, 0px) + clamp(12px, 3.5vw, 34px))
    calc(env(safe-area-inset-right, 0px) + 16px)
    calc(env(safe-area-inset-bottom, 0px) + 8px)
    calc(env(safe-area-inset-left, 0px) + 16px);
}

.rays {
  position: absolute; inset: 0; pointer-events: none;
  background:
    linear-gradient(100deg, transparent 40%, rgba(160, 220, 255, 0.08) 46%, transparent 52%),
    linear-gradient(80deg, transparent 55%, rgba(160, 220, 255, 0.06) 60%, transparent 66%);
  mix-blend-mode: screen;
  animation: shimmer 9s ease-in-out infinite alternate;
}
@keyframes shimmer { from { opacity: .5; transform: translateX(-3%);} to { opacity: 1; transform: translateX(3%);} }

.bubbles { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.bubbles i {
  position: absolute; bottom: -30px; border-radius: 50%;
  background: radial-gradient(circle at 32% 30%, rgba(255,255,255,.9), rgba(190,230,250,.25));
  animation: rise linear infinite;
}
@keyframes rise {
  0%   { transform: translateY(0) translateX(0); opacity: 0; }
  10%  { opacity: .7; }
  90%  { opacity: .5; }
  100% { transform: translateY(-102vh) translateX(var(--drift, 20px)); opacity: 0; }
}

/* ══════ layout ══════ */
.stage {
  position: relative; z-index: 2;
  width: 100%; max-width: 540px;
  display: flex; flex-direction: column; align-items: center;
  gap: clamp(10px, 1.6vh, 16px);
}

.brand { display: flex; align-items: center; gap: 14px; text-align: left; width: 100%; }
.brand-logo { width: 48px; height: 48px; object-fit: contain; flex: none; filter: drop-shadow(0 2px 8px rgba(0,0,0,.4)); }
.brand-text h1 { margin: 0; font-size: clamp(20px, 5vw, 28px); letter-spacing: .5px; }
.brand-text p { margin: 2px 0 0; font-size: 13px; color: #9fc6e4; }
.lang-toggle {
  margin-left: auto; align-self: center; flex: none;
  font: inherit; font-weight: 600; font-size: 13px; cursor: pointer;
  color: var(--foam); background: rgba(255,255,255,.08);
  border: 1px solid rgba(150,200,235,.32); border-radius: 999px;
  padding: 6px 14px; transition: background .15s, border-color .15s;
}
.lang-toggle:hover { background: rgba(255,255,255,.16); border-color: var(--cyan); }

/* ══════ วาฬ ══════ */
.whale-wrap {
  position: relative;
  width: min(72vw, 26vh, 320px);
  height: min(72vw, 26vh, 320px);
  display: grid; place-items: center;
  cursor: pointer;
  outline: none;
  transition: transform .3s ease;
}
.whale-front, .whale-side { position: absolute; width: 100%; height: 100%; overflow: visible; }
.whale-side { opacity: 0; pointer-events: none; }

.whale-front { animation: bob 4s ease-in-out infinite; transform-origin: 50% 50%; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-1deg);} 50% { transform: translateY(-10px) rotate(1deg);} }

/* ปากหายใจ idle */
#mouth { transform-box: fill-box; transform-origin: 50% 35%; animation: breathe 3.2s ease-in-out infinite; }
@keyframes breathe { 0%,100% { transform: scaleY(1);} 50% { transform: scaleY(1.14);} }

.spout { opacity: 0; transform-origin: 210px 56px; }
.pupil { transition: transform .2s ease; }

/* hover/focus */
.whale-wrap:hover .whale-front, .whale-wrap:focus-visible .whale-front { transform: scale(1.03); }

.hint {
  text-align: center; font-size: 14px; color: #bcdcf2; line-height: 1.5;
  margin-top: -4px; text-shadow: 0 1px 6px rgba(0,0,0,.5); pointer-events: none;
}
.hint .hint-sub { font-size: 12px; color: #8fb6d6; }

/* ── state: dragover ── */
.stage[data-state="dragover"] .whale-wrap { transform: scale(1.05); }
.stage[data-state="dragover"] #mouth { animation: none; transform: scaleY(1.45); filter: drop-shadow(0 0 16px var(--cyan)); }
.stage[data-state="dragover"] .hint { color: var(--cyan); }

/* ── state: chomp ── */
.stage[data-state="chomp"] #mouth { animation: chomp .18s ease-in forwards; }
.stage[data-state="chomp"] .whale-front { animation: shake .35s ease; }
@keyframes chomp { 0% { transform: scaleY(1.45);} 55% { transform: scaleY(.08);} 100% { transform: scaleY(.16);} }
@keyframes shake { 0%,100%{transform:translateX(0)} 20%{transform:translateX(-8px) rotate(-2deg)} 40%{transform:translateX(7px) rotate(2deg)} 60%{transform:translateX(-5px)} 80%{transform:translateX(4px)} }

/* ── state: uploading ── */
.stage[data-state="uploading"] .whale-front { animation: sway 2.6s ease-in-out infinite; }
.stage[data-state="uploading"] #mouth { animation: none; transform: scaleY(.16); }
.stage[data-state="uploading"] .spout { animation: spout 2.6s ease-in-out infinite; }
@keyframes sway { 0%,100%{transform:translateY(0) rotate(-2deg)} 50%{transform:translateY(-6px) rotate(2deg)} }
@keyframes spout { 0%,60%{opacity:0; transform:translateY(6px) scaleY(.4)} 78%{opacity:1; transform:translateY(0) scaleY(1)} 100%{opacity:0; transform:translateY(-6px) scaleY(1.1)} }
#wave { animation: waveMove 1.6s linear infinite; }
@keyframes waveMove { from { transform: translateX(0);} to { transform: translateX(120px);} }

/* ── state: depart — งับหน้าตรง แล้วหมุนเป็นมุมข้าง ว่ายไปไกลลิบ ── */
.stage[data-state="depart"] .whale-front { animation: squishOut .5s ease-in forwards; }
.stage[data-state="depart"] .whale-side { animation: swimAway 3.4s cubic-bezier(.4,0,.2,1) .35s forwards; }
@keyframes squishOut { 0%{opacity:1; transform:scaleX(1)} 100%{opacity:0; transform:scaleX(.2) rotate(-6deg)} }
@keyframes swimAway {
  0%   { opacity: 0; transform: translate(0,0) scale(1) rotate(0deg); }
  12%  { opacity: 1; }
  60%  { opacity: 1; transform: translate(46vw,-9vh) scale(.34) rotate(-6deg); }
  100% { opacity: 0; transform: translate(80vw,-16vh) scale(.05) rotate(-10deg); }
}
.stage[data-state="depart"] .whale-side .tail { animation: fluke .5s ease-in-out infinite; transform-box: fill-box; transform-origin: 80% 50%; }
@keyframes fluke { 0%,100%{transform:rotate(-8deg)} 50%{transform:rotate(10deg)} }

/* ── state: done — ซ่อนวาฬ ── */
.stage[data-state="done"] .whale-wrap { display: none; }
.stage[data-state="uploading"] .hint,
.stage[data-state="depart"] .hint,
.stage[data-state="done"] .hint { display: none; }

/* ══════ panels ══════ */
.panel {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--panel-brd);
  border-radius: var(--radius);
  padding: clamp(14px, 2vh, 18px);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.form { display: flex; flex-direction: column; gap: clamp(8px, 1.3vh, 12px); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: end; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: #bcdcf2; }
label > .lbl { display: block; line-height: 1.3; white-space: nowrap; }
.req { color: var(--cyan); }
input[type="text"], input[type="password"], input[type="number"], input:not([type]) {
  font: inherit; color: #fff;
  background: rgba(255,255,255,.06);
  border: 1px solid rgba(150,200,235,.28);
  border-radius: 10px; padding: clamp(8px, 1.2vh, 11px) 13px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: #6f96b6; }
input:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(94,208,255,.18); }
input:-webkit-autofill, input:-webkit-autofill:hover, input:-webkit-autofill:focus {
  -webkit-text-fill-color: #fff;
  -webkit-box-shadow: 0 0 0 40px #123050 inset;
  caret-color: #fff;
}

.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border-radius: 11px; padding: clamp(10px, 1.4vh, 13px) 18px; border: none;
  transition: transform .12s, box-shadow .2s, background .2s;
}
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(135deg, #37b6f0, #1f7fd0);
  color: #04121f; font-weight: 700;
  box-shadow: 0 6px 20px rgba(31,127,208,.4);
}
.btn.primary:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }
.btn.ghost {
  background: rgba(255,255,255,.07); color: var(--foam);
  border: 1px solid rgba(150,200,235,.3);
}
.btn.ghost:hover { background: rgba(255,255,255,.13); }

.filelist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.filelist li {
  display: flex; align-items: center; gap: 10px;
  background: rgba(255,255,255,.05); border: 1px solid rgba(150,200,235,.16);
  border-radius: 10px; padding: 9px 12px; font-size: 13px;
}
.filelist .fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filelist .fsize { color: #8fb6d6; font-size: 12px; white-space: nowrap; }
.filelist .fdel { cursor: pointer; color: #8fb6d6; background: none; border: none; font-size: 18px; line-height: 1; padding: 0 4px; }
.filelist .fdel:hover { color: var(--err); }
.filelist li.ok { border-color: rgba(90,220,160,.4); }
.filelist .fstat { font-size: 12px; color: #6fe3b0; }

.err { color: var(--err); font-size: 13px; margin: 2px 0 0; text-align: center; }

/* progress */
.progress-panel {
  width: 100%; background: var(--panel); border: 1px solid var(--panel-brd);
  border-radius: var(--radius); padding: 16px 18px; display: none;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.stage[data-state="uploading"] .progress-panel { display: block; }
.progress-head { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 10px; }
.progress-head #progressPct { color: var(--cyan); }
.bar { height: 12px; border-radius: 8px; background: rgba(255,255,255,.09); overflow: hidden; }
.bar-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #37b6f0, #7ee7ff);
  border-radius: 8px; transition: width .25s ease;
  box-shadow: 0 0 12px rgba(94,208,255,.6);
}
.progress-sub { display: flex; justify-content: space-between; margin-top: 8px; font-size: 12px; color: #9fc6e4; gap: 12px; }
.progress-sub #progressFile { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* done */
.done-card { text-align: center; display: none; animation: fadeUp .5s ease; }
.stage[data-state="done"] .done-card { display: block; }
.stage[data-state="done"] .form,
.stage[data-state="done"] .progress-panel { display: none; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(16px);} to { opacity: 1; transform: none;} }
.done-emoji { font-size: 40px; }
.done-whale { width: 56px; height: 56px; object-fit: contain; vertical-align: -14px; }
.done-card h2 { font-size: 20px; margin: 6px 0 14px; }
.done-card .filelist { margin-bottom: 14px; text-align: left; }
.drive-link {
  display: inline-block; background: #0e3a5c; color: var(--foam);
  padding: 10px 18px; border-radius: 10px; text-decoration: none; margin-bottom: 12px;
  border: 1px solid var(--panel-brd);
}
.drive-link:hover { background: #14507e; }
.done-card .btn { width: 100%; }

/* progress panel visible only while uploading; keep whale + form arrangement */
.stage[data-state="uploading"] .form { display: none; }

.foot { position: relative; z-index: 2; margin-top: clamp(6px, 1.5vh, 22px); font-size: 11px; color: #5f88a8; text-align: center; }

/* mobile */
@media (max-width: 480px) {
  .stage { gap: 10px; max-width: 460px; }
  .brand { gap: 10px; }
  .brand-logo { width: 42px; height: 42px; }
  .brand-text h1 { font-size: 19px; }
  .brand-text p { font-size: 11px; }
  .lang-toggle { padding: 5px 11px; font-size: 12px; }
  .whale-wrap { width: min(74vw, 300px); height: min(74vw, 300px); }
  .admin-upload .whale-wrap { width: min(58vw, 224px); height: min(58vw, 224px); }
  .back-admin-btn { padding: 7px 13px; font-size: 12px; margin-bottom: 2px; }
  .hint { font-size: 13px; margin-top: -2px; }
  .hint .hint-sub { font-size: 11px; }
  .panel, .form, .progress-panel { padding: 13px; }
  .form { gap: 8px; }
  .row2 { gap: 8px; }
  .lbl { white-space: normal; }
  label { font-size: 12px; gap: 4px; }
  input:not([type]), input[type="text"] { padding: 9px 11px; border-radius: 9px; }
  .btn { padding: 10px 14px; border-radius: 10px; }
  .foot { margin-top: 8px; font-size: 10px; }
}

/* จอเตี้ยมากๆ (เช่น iPhone SE) — ย่อวาฬเล็กน้อยพอให้ไม่ต้องเลื่อน */
@media (max-width: 480px) and (max-height: 720px) {
  .whale-wrap { width: min(56vw, 220px); height: min(56vw, 220px); }
  .stage { gap: 7px; }
  .brand-logo { width: 38px; height: 38px; }
}

/* ══════ auth: login / closed / admin ══════ */
#screenUpload { width: 100%; display: flex; flex-direction: column; align-items: center; gap: clamp(10px, 1.6vh, 16px); }

.logout-btn {
  flex: none; font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
  color: #ffb3c0; background: rgba(255,120,140,.12);
  border: 1px solid rgba(255,140,160,.35); border-radius: 999px; padding: 6px 12px;
  transition: background .15s;
}
.logout-btn:hover { background: rgba(255,120,140,.22); }

.login-card, .closed-card { width: 100%; max-width: 380px; text-align: center; animation: fadeUp .4s ease; }
.login-emoji { width: 66px; height: 66px; object-fit: contain; margin-bottom: 2px; filter: drop-shadow(0 2px 8px rgba(0,0,0,.4)); }
.login-card h2, .closed-card h2, .admin-card h2 { font-size: 21px; margin: 6px 0; }
.login-sub { font-size: 13px; color: #9fc6e4; margin: 0 0 16px; }
#loginForm { display: flex; flex-direction: column; gap: 12px; text-align: left; }
#loginForm .btn { margin-top: 4px; }
.remember-row { display: flex; flex-direction: row; align-items: center; justify-content: flex-start; gap: 8px; font-size: 13px; color: #bcdcf2; cursor: pointer; user-select: none; }
.remember-row input[type="checkbox"] { flex: none; width: 17px; height: 17px; accent-color: #37b6f0; cursor: pointer; margin: 0; }
.remember-row span { line-height: 1.2; }

.closed-card { max-width: 400px; }
.closed-emoji { font-size: 46px; }
.closed-card p { font-size: 14px; color: #bcdcf2; line-height: 1.6; margin: 6px 0 16px; }

.admin-card { width: 100%; max-width: 480px; text-align: left; animation: fadeUp .4s ease; }
.admin-card h2 { text-align: center; margin-bottom: 12px; }
.go-upload-btn { width: 100%; margin-bottom: 14px; }
#goUploadBtn::before, #sendBtn::before {
  content: ""; display: inline-block; width: 1.35em; height: 1.35em;
  background: url(/whale.png?v=1) center/contain no-repeat;
  vertical-align: -0.35em; margin-right: 7px;
}
.back-admin-btn { align-self: flex-start; font-size: 13px; padding: 8px 15px; margin-bottom: 4px; }
.pipe-box {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: rgba(255,255,255,.05); border: 1px solid rgba(150,200,235,.2);
  border-radius: 12px; padding: 14px 16px;
}
.pipe-status { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; }
.pipe-dot { width: 12px; height: 12px; border-radius: 50%; background: #ff6b6b; box-shadow: 0 0 10px #ff6b6b; transition: background .2s, box-shadow .2s; }
.pipe-dot.open { background: #4ade80; box-shadow: 0 0 10px #4ade80; }
.pipe-toggle { background: linear-gradient(135deg, #37b6f0, #1f7fd0); color: #04121f; font-weight: 700; white-space: nowrap; }
.pipe-toggle.is-open { background: rgba(255,110,120,.16); color: #ffb3c0; border: 1px solid rgba(255,140,160,.4); }
.admin-hint { font-size: 12px; color: #8fb6d6; margin: 10px 2px 16px; line-height: 1.5; }
.admin-log { margin-top: 16px; padding-top: 14px; border-top: 1px solid rgba(150,200,235,.14); }
.admin-log-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.mini-btn { font: inherit; font-size: 12px; cursor: pointer; color: var(--cyan); background: none; border: none; padding: 2px 4px; }
#adminLogList li { flex-direction: column; align-items: flex-start; gap: 2px; font-size: 12px; }
#adminLogList .log-meta { color: #8fb6d6; font-size: 11px; }
#adminLogList .log-empty { color: #6f96b6; font-size: 12px; }

.scan-note { font-size: 12px; color: #8fb6d6; text-align: center; margin: 4px 0 0; }
.scan-note.bad { color: var(--err); }

.admin-section { margin-top: 16px; padding-top: 14px; border-top: 1px solid rgba(150,200,235,.14); }
.admin-sec-title { font-weight: 600; font-size: 14px; margin-bottom: 9px; }
.inline-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.inline-row .unit { color: #9fc6e4; font-size: 13px; }
.inline-row input[type="number"] { width: 96px; }
.inline-row.adduser input { flex: 1; min-width: 110px; }
.btn.small { padding: 9px 15px; font-size: 13px; border-radius: 9px; background: rgba(255,255,255,.09); color: var(--foam); border: 1px solid rgba(150,200,235,.3); white-space: nowrap; }
.btn.small:hover { background: rgba(255,255,255,.16); }
.admin-note { font-size: 11px; color: #8fb6d6; margin: 8px 2px 0; line-height: 1.5; }
.sel {
  font: inherit; color: #fff; background: rgba(255,255,255,.06);
  border: 1px solid rgba(150,200,235,.28); border-radius: 10px;
  padding: 9px 12px; outline: none; width: 100%; cursor: pointer;
}
.sel:focus { border-color: var(--cyan); }
.sel option { color: #000; }

.pass-wrap { position: relative; display: block; }
.pass-wrap input { width: 100%; padding-right: 42px; }
.pass-eye {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; color: #bcdcf2;
  opacity: .8; padding: 4px; border-radius: 6px; display: inline-flex; align-items: center;
}
.pass-eye:hover { opacity: 1; background: rgba(255,255,255,.08); }
.pass-eye svg { width: 20px; height: 20px; display: block; }
.inline-row .pass-wrap { flex: 1; min-width: 120px; }
.user-list { margin-bottom: 10px; }
.user-list:empty { display: none; }
.user-list li .fname { font-size: 13px; }

@media (prefers-reduced-motion: reduce) {
  .whale-front, #mouth, .rays, #wave, .bubbles i, .spout { animation: none !important; }
}
