/* Type scale (Material 3) */
:root {
  --font: "Roboto", "Noto Sans Thai", system-ui, sans-serif;
  --top-bar-h: 64px;
  --nav-h: 80px;
  --rail-w: 80px;
  --drawer-w: 280px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--md-sys-color-surface);
  color: var(--md-sys-color-on-surface);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.5rem;
  letter-spacing: 0.03125rem;
}

h1, h2, p, dl, dd { margin: 0; }

a { color: var(--md-sys-color-primary); }

.headline-medium { font-size: 1.75rem; line-height: 2.25rem; font-weight: 400; letter-spacing: 0; }
.title-large { font-size: 1.375rem; line-height: 1.75rem; font-weight: 400; letter-spacing: 0; }
.body-large { font-size: 1rem; line-height: 1.5rem; }
.body-small { font-size: 0.75rem; line-height: 1rem; letter-spacing: 0.025rem; }
.label-large { font-size: 0.875rem; line-height: 1.25rem; font-weight: 500; letter-spacing: 0.00625rem; }
.muted { color: var(--md-sys-color-on-surface-variant); }

/* Top app bar */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: calc(var(--top-bar-h) + var(--safe-top));
  padding: var(--safe-top) 12px 0 16px;
  background: var(--md-sys-color-surface);
}

.top-bar-title {
  font-size: 1.375rem;
  line-height: 1.75rem;
  font-weight: 500;
  letter-spacing: 0;
  color: var(--md-sys-color-primary);
  text-decoration: none;
}

.top-bar-actions { display: flex; align-items: center; gap: 4px; }
.top-bar-actions form { margin: 0; }

/* EN/TH: Material 3 segmented button group */
.lang-switch {
  display: flex;
  margin-right: 8px;
  border: 1px solid var(--md-sys-color-outline);
  border-radius: 20px;
  overflow: hidden;
}

.lang-switch form + form { border-left: 1px solid var(--md-sys-color-outline); }

.lang-option {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 48px;
  height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--md-sys-color-on-surface);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s linear, color 0.2s linear;
}

.lang-option:hover { background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent); }
.lang-option:focus-visible { outline: 3px solid var(--md-sys-color-secondary); outline-offset: -3px; }
.lang-option.is-current {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
}
.lang-option.is-current::before {
  content: "\e5ca";
  font-family: "Material Symbols Outlined";
  font-size: 18px;
  font-weight: 400;
  line-height: 1;
  font-feature-settings: "liga";
}

/* Content */
.content {
  max-width: 840px;
  margin: 0 auto;
  padding: 16px 16px 32px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 16px;
}

.content.has-nav { padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 24px); }

@media print {
  .top-bar, .nav, .no-print { display: none !important; }
  .content, .content.has-nav { max-width: none; padding: 0; }
  body { background: #fff; color: #000; }
}

/* Navigation: bar on compact, rail on medium, drawer on expanded */
.nav {
  position: fixed;
  z-index: 9;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding: 12px 8px calc(16px + var(--safe-bottom));
  background: var(--md-sys-color-surface-container);
}

.nav-item {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--md-sys-color-on-surface-variant);
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}

.nav-indicator {
  position: relative;
  display: grid;
  place-items: center;
  width: 64px;
  height: 32px;
  border-radius: 16px;
  overflow: hidden;
  font-size: 1.125rem;
}

.nav-label { font-size: 0.75rem; line-height: 1rem; font-weight: 500; letter-spacing: 0.03125rem; }

.nav-item.is-active { color: var(--md-sys-color-on-surface); }
.nav-item.is-active .nav-indicator {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
}
.nav-item:focus-visible { outline: 3px solid var(--md-sys-color-secondary); outline-offset: 2px; border-radius: 16px; }

@media (min-width: 600px) {
  .nav {
    top: calc(var(--top-bar-h) + var(--safe-top));
    right: auto;
    width: var(--rail-w);
    height: auto;
    flex-direction: column;
    justify-content: flex-start;
    gap: 12px;
    padding: 12px 0;
    background: var(--md-sys-color-surface);
  }
  .nav-item { flex: 0 0 auto; }
  .content.has-nav { margin-left: var(--rail-w); padding-bottom: 32px; max-width: none; }
  .content { padding: 24px; }
}

@media (min-width: 1200px) {
  .nav {
    width: var(--drawer-w);
    padding: 12px;
    gap: 0;
  }
  .nav-item {
    flex-direction: row;
    align-items: center;
    gap: 12px;
    height: 56px;
    padding: 0 16px;
    border-radius: 28px;
    overflow: hidden;
  }
  .nav-indicator { width: 24px; height: 24px; border-radius: 0; background: none !important; overflow: visible; }
  .nav-item.is-active { background: var(--md-sys-color-secondary-container); color: var(--md-sys-color-on-secondary-container); }
  .nav-label { font-size: 0.875rem; line-height: 1.25rem; letter-spacing: 0.00625rem; }
  .content.has-nav { margin-left: var(--drawer-w); padding-left: 24px; }
  .content.has-nav > * { max-width: 760px; }
}

/* Facts list */
.facts {
  width: 100%;
  max-width: 640px;
  background: var(--md-sys-color-surface-container-low);
  border-radius: 12px;
  padding: 4px 16px;
}

.facts > div { padding: 12px 0; border-bottom: 1px solid var(--md-sys-color-outline-variant); }
.facts > div:last-child { border-bottom: 0; }
.facts dt { font-size: 0.75rem; line-height: 1rem; letter-spacing: 0.03125rem; color: var(--md-sys-color-on-surface-variant); }
.facts dd { margin-top: 2px; }

/* Notices */
.notice {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 640px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
  font-size: 0.875rem;
  line-height: 1.25rem;
}

.notice > span { flex: 1; }
.notice > span a { color: inherit; font-weight: 600; text-decoration: underline; margin-left: 4px; }
.notice-error { background: var(--md-sys-color-error-container); color: var(--md-sys-color-on-error-container); }

/* Sign in */
.auth-page .content { max-width: none; padding-top: 32px; align-items: center; }

.auth-panel {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.stack { display: flex; flex-direction: column; gap: 16px; }
.stack md-outlined-text-field { width: 100%; }
.stack md-filled-button { align-self: flex-start; }

md-outlined-text-field, md-filled-button, md-filled-tonal-button, md-text-button, md-icon-button {
  font-family: var(--font);
}

[hidden] { display: none !important; }

/* Icons: Material Symbols Outlined with an animatable FILL axis */
.ms {
  display: inline-block;
  font-family: "Material Symbols Outlined";
  font-weight: 400;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  white-space: nowrap;
  direction: ltr;
  user-select: none;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "liga";
  font-variation-settings: "FILL" 0;
  transition: font-variation-settings 0.25s cubic-bezier(0.2, 0, 0, 1), transform 0.2s cubic-bezier(0.2, 0, 0, 1);
}

md-icon-button:hover .ms,
md-icon-button:focus-visible .ms { font-variation-settings: "FILL" 1; }
md-icon-button:active .ms { transform: scale(0.86); }

.nav-item:hover .ms,
.nav-item.is-active .ms { font-variation-settings: "FILL" 1; }
.nav-item:active .ms { transform: scale(0.86); }
.nav-item.is-active .ms { animation: nav-pop 0.35s cubic-bezier(0.3, 1.4, 0.5, 1); }

.notice .ms { flex: none; font-variation-settings: "FILL" 1; }

/* Password visibility swap */
[data-toggle-password] .toggle-off { display: none; }
[data-toggle-password][data-visible] .toggle-on { display: none; }
[data-toggle-password][data-visible] .toggle-off { display: inline-block; }
[data-toggle-password] .ms { animation: icon-pop 0.25s cubic-bezier(0.3, 1.4, 0.5, 1); }

/* Top bar appearance toggle */
.mode-toggle {
  position: relative;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.mode-toggle:focus-visible { outline: 3px solid var(--md-sys-color-secondary); outline-offset: -2px; }
.mode-toggle:hover .ms { font-variation-settings: "FILL" 1; }

.mode-glyph {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
  transform: rotate(-90deg) scale(0.4);
  transition: transform 0.4s cubic-bezier(0.2, 0, 0, 1), opacity 0.2s linear;
  pointer-events: none;
}

html[data-mode="system"] .mode-glyph[data-for="system"],
html[data-mode="light"] .mode-glyph[data-for="light"],
html[data-mode="dark"] .mode-glyph[data-for="dark"] { opacity: 1; transform: none; }

.mode-glyph[data-for="light"].is-playing .ms { animation: spin-sun 0.6s cubic-bezier(0.2, 0, 0, 1); }
.mode-glyph[data-for="dark"].is-playing .ms { animation: swing-moon 0.55s cubic-bezier(0.3, 1.4, 0.5, 1); }
.mode-glyph[data-for="system"].is-playing .ms { animation: pulse-auto 0.5s ease-out; }

/* Preferences */
.title-medium { font-size: 1rem; line-height: 1.5rem; font-weight: 500; letter-spacing: 0.009375rem; }
.body-medium { font-size: 0.875rem; line-height: 1.25rem; letter-spacing: 0.015625rem; }

.pref-group {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  width: 100%;
  max-width: 640px;
  margin-top: 8px;
}

.pref-group .muted { margin-bottom: 4px; }

.segmented {
  display: flex;
  max-width: 100%;
  border: 1px solid var(--md-sys-color-outline);
  border-radius: 20px;
  overflow: hidden;
}

.segment { position: relative; display: block; }
.segment + .segment { border-left: 1px solid var(--md-sys-color-outline); }

.segment input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.segment-body {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px;
  color: var(--md-sys-color-on-surface);
  transition: background-color 0.2s linear, color 0.2s linear;
}

.segment-body .ms { font-size: 18px; }
.segment input:checked + .segment-body {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
}
.segment input:checked + .segment-body .ms,
.segment:hover .ms { font-variation-settings: "FILL" 1; }
.segment input:focus-visible + .segment-body { outline: 3px solid var(--md-sys-color-secondary); outline-offset: -3px; }

.segment-icon.anim-light.is-playing { animation: spin-sun 0.6s cubic-bezier(0.2, 0, 0, 1); }
.segment-icon.anim-dark.is-playing { animation: swing-moon 0.55s cubic-bezier(0.3, 1.4, 0.5, 1); }
.segment-icon.anim-system.is-playing { animation: pulse-auto 0.5s ease-out; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px 12px; }

.swatch-option {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 72px;
  padding: 4px 0;
  cursor: pointer;
}

.swatch-option input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.swatch-disc {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--swatch);
  color: #fff;
  outline: 3px solid transparent;
  outline-offset: 3px;
  transition: transform 0.2s cubic-bezier(0.2, 0, 0, 1), outline-color 0.2s linear;
}

.swatch-option:hover .swatch-disc { transform: scale(1.08); }
.swatch-option:active .swatch-disc { transform: scale(0.94); }
.swatch-option input:checked + .swatch-disc { outline-color: var(--swatch); }
.swatch-option input:focus-visible + .swatch-disc { outline-color: var(--md-sys-color-on-surface); }

.swatch-check {
  opacity: 0;
  transform: scale(0.3) rotate(-45deg);
  font-variation-settings: "wght" 700;
  transition: opacity 0.15s linear, transform 0.3s cubic-bezier(0.3, 1.4, 0.5, 1);
}

.swatch-option input:checked + .swatch-disc .swatch-check { opacity: 1; transform: none; }
.swatch-check.is-playing { animation: icon-pop 0.35s cubic-bezier(0.3, 1.4, 0.5, 1); }

@keyframes spin-sun { from { transform: rotate(-180deg) scale(0.5); } to { transform: none; } }
@keyframes swing-moon { 0% { transform: rotate(-50deg) scale(0.5); } 100% { transform: none; } }
@keyframes pulse-auto { 0% { transform: scale(0.7); } 55% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes icon-pop { 0% { transform: scale(0.6); } 100% { transform: scale(1); } }
@keyframes nav-pop { 0% { transform: scale(0.8); } 100% { transform: scale(1); } }

@media (prefers-reduced-motion: reduce) {
  .ms, .mode-glyph, .swatch-disc, .swatch-check, .segment-body { transition: none !important; }
  .ms, .segment-icon, .swatch-check { animation: none !important; }
}
