:root {
  color-scheme: light;
  --ink: #351456;
  --muted: #5d385f;
  --violet: #7c2cff;
  --violet-deep: #5d20d7;
  --cream: #fff7e8;
  --peach: #f4be87;
  --peach-light: #f8d7bd;
  --danger: #861c36;
  --success: #0b5b3b;
  --focus: #fff0a8;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", Tahoma, system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.login-scene {
  position: relative;
  isolation: isolate;
  display: grid;
  min-height: 100vh;
  min-height: 100dvh;
  place-items: center;
  overflow: auto;
  padding: max(18px, var(--safe-top)) max(18px, var(--safe-right)) max(18px, var(--safe-bottom)) max(18px, var(--safe-left));
  background: #27163b;
}

.login-scene::before {
  position: fixed;
  z-index: -2;
  inset: 0;
  content: "";
  background-image: url("./assets/login-village-v2.png");
  background-position: center center;
  background-size: cover;
  filter: saturate(1.02) contrast(1.01);
}

.login-scene::after {
  position: fixed;
  z-index: -1;
  inset: 0;
  content: "";
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 52%, rgba(53, 22, 78, .12), transparent 31%),
    linear-gradient(180deg, rgba(25, 11, 47, .06), rgba(25, 11, 47, .12));
}

.local-badge {
  position: fixed;
  z-index: 3;
  top: max(14px, var(--safe-top));
  left: max(14px, var(--safe-left));
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  margin: 0;
  padding: 6px 11px;
  border: 1px solid rgba(255, 240, 196, .46);
  border-radius: 999px;
  color: #fff8d9;
  background: rgba(43, 20, 69, .76);
  box-shadow: 0 8px 28px rgba(24, 10, 42, .25);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  backdrop-filter: blur(8px);
}

.local-badge span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ffe16b;
  box-shadow: 0 0 0 4px rgba(255, 225, 107, .17);
}

.login-card {
  position: relative;
  isolation: isolate;
  width: clamp(320px, 22vw, 366px);
  min-height: clamp(442px, 53.5vh, 500px);
  overflow: hidden;
  padding: clamp(34px, 5.8vh, 56px) 32px 24px;
  border: 1px solid rgba(255, 249, 224, .7);
  border-radius: 23px;
  color: var(--ink);
  background: linear-gradient(155deg, rgba(255, 246, 231, .54), rgba(244, 190, 135, .36));
  box-shadow: 0 25px 74px rgba(40, 17, 56, .36), inset 0 1px rgba(255, 255, 255, .72);
  -webkit-backdrop-filter: blur(16px) saturate(1.14);
  backdrop-filter: blur(16px) saturate(1.14);
}

.login-card::before {
  position: absolute;
  z-index: -1;
  inset: 1px;
  border-radius: 21px;
  content: "";
  pointer-events: none;
  background: linear-gradient(145deg, rgba(255, 255, 255, .2), transparent 43%, rgba(255, 211, 152, .12));
}

.brand-block {
  display: grid;
  min-height: clamp(118px, 17vh, 154px);
  place-items: center;
  margin: -6px -32px 20px;
  border-bottom: 1px solid rgba(255, 250, 233, .4);
}

.brand-logo {
  display: block;
  width: min(310px, 96%);
  height: auto;
  margin-block: -24px;
  filter: drop-shadow(0 5px 8px rgba(51, 20, 74, .17));
}

.login-form {
  display: grid;
  gap: 11px;
}

.field {
  position: relative;
  min-width: 0;
}

.field input {
  width: 100%;
  height: 48px;
  min-width: 0;
  padding: 15px 49px 4px 43px;
  border: 1px solid rgba(142, 74, 200, .23);
  border-radius: 999px;
  outline: 0;
  color: var(--ink);
  background: rgba(255, 249, 236, .94);
  box-shadow: inset 0 1px 3px rgba(95, 55, 70, .08), 0 1px rgba(255, 255, 255, .7);
  font-size: 16px;
  transition: border-color .18s ease, box-shadow .18s ease, background .18s ease;
}

.field-label {
  position: absolute;
  top: 50%;
  right: 48px;
  left: 43px;
  overflow: hidden;
  color: #744380;
  font-size: 13px;
  line-height: 1;
  pointer-events: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  transform: translateY(-50%);
  transform-origin: left center;
  transition: top .16s ease, color .16s ease, font-size .16s ease, transform .16s ease;
}

.field input:focus + .field-label,
.field input:not(:placeholder-shown) + .field-label {
  top: 9px;
  color: #744380;
  font-size: 9px;
  font-weight: 800;
  transform: none;
}

.field input:focus-visible {
  border-color: var(--violet);
  background: #fffdf5;
  box-shadow: 0 0 0 3px rgba(255, 240, 168, .9), 0 0 0 5px rgba(97, 39, 173, .7);
}

.field input[aria-invalid="true"] {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(163, 38, 67, .15);
}

.field-icon {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 15px;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: #8a3cc2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
  pointer-events: none;
  transform: translateY(-50%);
}

.password-toggle {
  position: absolute;
  z-index: 2;
  top: 2px;
  right: 4px;
  display: grid;
  width: 44px;
  height: 44px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 50%;
  color: #8738bd;
  background: transparent;
  cursor: pointer;
}

.password-toggle svg {
  width: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
}

.password-toggle:hover {
  background: rgba(124, 44, 255, .08);
}

.password-toggle:focus-visible,
.forgot-button:focus-visible,
.help-button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 5px rgba(78, 28, 137, .72);
}

.form-status {
  min-height: 18px;
  margin: -3px 3px -4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
  line-height: 1.35;
  text-align: center;
}

.form-status[data-kind="error"] {
  color: var(--danger);
}

.form-status[data-kind="success"] {
  color: var(--success);
}

.login-button {
  position: relative;
  display: flex;
  width: 100%;
  min-height: 49px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  overflow: hidden;
  border: 0;
  border-radius: 999px;
  color: #fff;
  background: linear-gradient(105deg, var(--violet-deep), #8f32f0 70%, #7a27eb);
  box-shadow: 0 8px 18px rgba(104, 36, 198, .35), inset 0 1px rgba(255, 255, 255, .24);
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease, filter .18s ease;
}

.login-button:hover:not(:disabled) {
  filter: brightness(1.06);
  transform: translateY(-1px);
  box-shadow: 0 10px 23px rgba(104, 36, 198, .43), inset 0 1px rgba(255, 255, 255, .28);
}

.login-button:active:not(:disabled) {
  transform: translateY(1px);
}

.login-button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.login-button:disabled {
  cursor: wait;
  filter: saturate(.7);
}

.button-loader {
  display: none;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, .45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

.login-form[aria-busy="true"] .button-spark {
  display: none;
}

.login-form[aria-busy="true"] .button-loader {
  display: block;
}

.forgot-button {
  justify-self: center;
  min-width: 112px;
  min-height: 44px;
  margin: -6px 0 -9px;
  padding: 8px;
  border: 0;
  border-radius: 10px;
  color: #77366f;
  background: transparent;
  font-size: 11px;
  cursor: pointer;
}

.forgot-button:hover {
  color: #4f205d;
  text-decoration: underline;
}

.recovery-backdrop {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  padding: max(12px, var(--safe-top)) max(12px, var(--safe-right)) max(12px, var(--safe-bottom)) max(12px, var(--safe-left));
  place-items: center;
  background: rgba(29, 12, 47, .72);
}

.recovery-dialog {
  position: relative;
  width: min(430px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  padding: 25px 24px 22px;
  border: 1px solid rgba(255, 244, 207, .78);
  border-radius: 22px;
  color: var(--ink);
  background: linear-gradient(155deg, #fff9ec, #f7cfaa);
  box-shadow: 0 26px 80px rgba(20, 8, 37, .52), inset 0 1px #fff;
  overscroll-behavior: contain;
}

.recovery-dialog h2 {
  margin: 0 42px 7px 0;
  color: #4f205d;
  font-size: 23px;
  line-height: 1.2;
}

.recovery-dialog h2:focus { outline: 0; }

.recovery-close {
  position: absolute;
  top: 8px;
  right: 8px;
  display: grid;
  width: 44px;
  height: 44px;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 50%;
  color: #5d3865;
  background: transparent;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}

.recovery-close:hover { background: rgba(124, 44, 255, .08); }

.recovery-close:focus-visible,
.recovery-secondary:focus-visible,
.recovery-code:focus-visible,
.recovery-confirm input:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 5px rgba(78, 28, 137, .72);
}

.recovery-body,
.recovery-form,
.recovery-actions,
.recovery-success {
  display: grid;
  gap: 12px;
}

.recovery-intro,
.recovery-footnote,
.recovery-welcome-hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.recovery-footnote,
.recovery-welcome-hint { text-align: center; }

.recovery-form label,
.recovery-current-password {
  display: grid;
  gap: 5px;
  color: #5d3865;
  font-size: 12px;
  font-weight: 800;
}

.recovery-form input:not([type="checkbox"]),
.recovery-current-password input {
  width: 100%;
  min-height: 46px;
  padding: 10px 13px;
  border: 1px solid rgba(142, 74, 200, .28);
  border-radius: 13px;
  outline: 0;
  color: var(--ink);
  background: #fffdf7;
  font-size: 16px;
}

.recovery-form input:focus-visible,
.recovery-current-password input:focus-visible {
  border-color: var(--violet);
  box-shadow: 0 0 0 3px rgba(255, 240, 168, .9), 0 0 0 5px rgba(97, 39, 173, .7);
}

.recovery-secondary {
  min-height: 44px;
  padding: 8px 13px;
  border: 1px solid rgba(105, 45, 139, .24);
  border-radius: 999px;
  color: #612b70;
  background: rgba(255, 250, 237, .7);
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.recovery-secondary:hover { background: #fffaf0; }

.recovery-code {
  display: block;
  overflow-wrap: anywhere;
  padding: 15px 12px;
  border: 2px dashed rgba(105, 45, 139, .45);
  border-radius: 14px;
  color: #431653;
  background: #fffdf7;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: clamp(16px, 4.3vw, 21px);
  font-weight: 900;
  letter-spacing: .06em;
  line-height: 1.45;
  text-align: center;
}

.recovery-code-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.recovery-confirm {
  display: flex;
  min-height: 44px;
  align-items: center;
  gap: 9px;
  color: #4f205d;
  font-size: 13px;
  font-weight: 800;
}

.recovery-confirm input { width: 20px; height: 20px; accent-color: var(--violet); }

.recovery-success { text-align: center; }
.recovery-success-icon {
  display: grid;
  width: 58px;
  height: 58px;
  margin: 2px auto;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  background: var(--success);
  font-size: 32px;
  font-weight: 900;
}

.recovery-success p { margin: 0 0 4px; line-height: 1.55; }

.recovery-setup-button { margin-bottom: -8px; }
.recovery-setup-button.recovery-needed {
  color: #59216c;
  background: rgba(255, 239, 170, .52);
  text-decoration: none;
}

.local-notice {
  margin: -8px 0 10px;
  padding: 5px 8px;
  border: 1px solid rgba(83, 42, 93, .18);
  border-radius: 999px;
  color: #59355c;
  background: rgba(255, 250, 235, .38);
  font-size: 10px;
  font-weight: 750;
  line-height: 1.35;
  text-align: center;
}

.help-button {
  position: fixed;
  z-index: 5;
  right: max(12px, var(--safe-right));
  bottom: max(12px, var(--safe-bottom));
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid rgba(53, 20, 86, .12);
  border-radius: 50%;
  color: #241334;
  background: rgba(255, 254, 247, .96);
  box-shadow: 0 8px 24px rgba(34, 16, 55, .27);
  font-size: 20px;
  font-weight: 800;
  cursor: pointer;
}

.help-panel {
  position: fixed;
  z-index: 4;
  right: max(12px, var(--safe-right));
  bottom: calc(max(12px, var(--safe-bottom)) + 52px);
  width: min(292px, calc(100vw - 24px));
  padding: 15px 17px;
  border: 1px solid rgba(255, 235, 178, .42);
  border-radius: 16px;
  color: #fff7e8;
  background: rgba(48, 22, 73, .94);
  box-shadow: 0 18px 45px rgba(28, 11, 44, .36);
  font-size: 12px;
  line-height: 1.55;
}

.help-panel p {
  margin: 5px 0 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* หน้าต้อนรับหลังล็อกอิน — ยืนยันว่าดึงข้อมูลเด็กจริงจากฐานเดิมได้ */
.welcome-block {
  display: grid;
  gap: 12px;
  text-align: center;
}

.welcome-hi {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
}

.welcome-name {
  margin: -4px 0 2px;
  color: var(--ink);
  font-size: 24px;
  font-weight: 800;
  line-height: 1.15;
}

.welcome-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  margin-bottom: 4px;
}

.ws-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 5px 13px;
  border: 1px solid rgba(142, 74, 200, .26);
  border-radius: 999px;
  color: var(--ink);
  background: rgba(255, 249, 236, .82);
  box-shadow: inset 0 1px rgba(255, 255, 255, .7);
  font-size: 14px;
  font-weight: 800;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 1100px) {
  .login-scene::before {
    background-position: 54% center;
  }

  .login-card {
    width: min(390px, calc(100vw - 48px));
    min-height: 480px;
  }
}

@media (orientation: portrait) {
  .login-scene {
    align-items: start;
    padding-top: max(54px, calc(var(--safe-top) + 28px));
    padding-bottom: max(66px, calc(var(--safe-bottom) + 28px));
  }

  .login-scene::before {
    background-position: 50% center;
  }

  .login-scene::after {
    background: linear-gradient(180deg, rgba(34, 17, 57, .22), rgba(34, 17, 57, .32));
  }

  .login-card {
    width: min(420px, calc(100vw - 32px));
    min-height: 0;
    margin-block: auto;
  }
}

@media (max-width: 640px) {
  .login-card {
    padding: 30px 24px 22px;
    border-radius: 21px;
    background: linear-gradient(155deg, rgba(255, 247, 235, .68), rgba(244, 190, 135, .5));
    -webkit-backdrop-filter: blur(14px) saturate(1.1);
    backdrop-filter: blur(14px) saturate(1.1);
  }

  .brand-block {
    min-height: 126px;
    margin-inline: -24px;
  }

  .brand-logo {
    width: min(300px, 96%);
    margin-block: -22px;
  }

  .local-badge {
    min-height: 30px;
    font-size: 10px;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .login-scene {
    align-items: start;
    padding-top: max(8px, var(--safe-top));
    padding-bottom: max(8px, var(--safe-bottom));
  }

  .login-scene::before {
    background-position: center 54%;
  }

  .login-card {
    width: min(370px, calc(100vw - 112px));
    min-height: 0;
    max-height: calc(100vh - max(16px, var(--safe-top)) - max(16px, var(--safe-bottom)));
    max-height: calc(100dvh - max(16px, var(--safe-top)) - max(16px, var(--safe-bottom)));
    margin: auto;
    overflow-y: auto;
    padding: 18px 25px 17px;
    overscroll-behavior: contain;
    scroll-padding-bottom: 18px;
  }

  .brand-block {
    min-height: 76px;
    margin: -4px -25px 11px;
  }

  .brand-logo {
    width: min(220px, 92%);
    margin-block: -18px;
  }

  .login-form {
    gap: 7px;
  }

  .field input {
    height: 44px;
  }

  .password-toggle {
    top: 0;
  }

  .login-button {
    min-height: 44px;
  }

  .form-status {
    min-height: 15px;
  }

  .local-notice { margin: -4px 0 7px; }
}

@media (max-width: 600px) and (orientation: landscape) {
  .local-badge {
    display: none;
  }

  .login-card {
    width: min(330px, calc(100vw - 76px));
  }
}

@media (max-height: 600px) {
  .login-scene {
    place-items: start center;
  }

  .recovery-backdrop {
    place-items: start center;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}

@supports (-webkit-touch-callout: none) {
  .login-card {
    background: linear-gradient(155deg, rgba(255, 247, 235, .84), rgba(244, 190, 135, .72));
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .local-badge {
    backdrop-filter: none;
    background: rgba(43, 20, 69, .92);
  }
}
