/* MeeGear shared styles — standalone, no framework.
   Design tokens first (semantic, per ui-ux-pro-max); components below.
   Thai-first typography: Prompt (headings) + Sarabun (body), loaded by pages. */

:root {
    /* brand + semantic colors */
    --maroon: #8c1515;
    --maroon-dark: #6d0f0f;
    --maroon-tint: #f7e9e9;
    --bg: #f7f4ef;
    --surface: #ffffff;
    --text: #262220;
    --muted-fg: #655e56;          /* >=4.5:1 on --bg and --surface */
    --success: #157a5a;           /* darkened for 4.5:1 text use */
    --success-tint: #e0f3ec;
    --danger: #c22945;
    --danger-tint: #fbe6ea;
    --border: #e2dbd0;
    --ring: #8c1515;

    /* spacing scale (4/8 rhythm) */
    --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px;
    --sp-5: 24px; --sp-6: 32px; --sp-7: 48px;

    /* radius + elevation */
    --r-sm: 8px; --r-md: 12px; --r-lg: 16px; --r-full: 999px;
    --shadow-1: 0 1px 3px rgba(60, 40, 20, .07);
    --shadow-2: 0 4px 16px rgba(60, 40, 20, .10);

    /* z-index scale */
    --z-sticky: 20; --z-fixed: 40; --z-modal: 100;

    --font-head: 'Prompt', -apple-system, 'Noto Sans Thai', 'Sukhumvit Set', sans-serif;
    --font-body: 'Sarabun', -apple-system, 'Noto Sans Thai', 'Sukhumvit Set', Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-body);
    font-size: 16px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3 { font-family: var(--font-head); line-height: 1.35; }

.wrap { max-width: 560px; margin: 0 auto; padding: var(--sp-4); }
.wrap-wide { max-width: 1100px; }

/* ---------------- hero ---------------- */
.hero { text-align: center; padding: var(--sp-5) 0 var(--sp-3); }
.hero .mascot { width: 96px; height: 96px; }
.hero h1 { margin: var(--sp-1) 0 0; color: var(--maroon); font-size: 1.85rem; font-weight: 700; letter-spacing: .01em; }
.hero .tagline { color: var(--muted-fg); font-size: .95rem; }

/* ---------------- surfaces ---------------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    padding: var(--sp-5);
    margin-bottom: var(--sp-4);
    box-shadow: var(--shadow-1);
}

h2 { font-size: 1.15rem; margin: 0 0 var(--sp-2); }
.muted { color: var(--muted-fg); font-size: .9rem; }
.small { font-size: .85rem; }
.center { text-align: center; }
.mt { margin-top: var(--sp-4); }
.hidden { display: none !important; }

/* ---------------- forms ---------------- */
label { display: block; font-weight: 600; margin-bottom: var(--sp-2); }
input[type=text], input[type=password], input[type=email], input[type=tel],
select, textarea {
    width: 100%;
    min-height: 48px;
    padding: var(--sp-3) var(--sp-4);
    border: 1.5px solid #c9c0b2;
    border-radius: var(--r-sm);
    font: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color .15s ease-out, box-shadow .15s ease-out;
}
textarea { min-height: 96px; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--ring);
    box-shadow: 0 0 0 3px rgba(140, 21, 21, .18);
}
input[aria-invalid="true"], .field-invalid input { border-color: var(--danger); }
.field { margin-bottom: var(--sp-4); }
.field-error {
    display: none;
    color: var(--danger);
    font-size: .85rem;
    margin-top: var(--sp-1);
}
.field-invalid .field-error { display: block; }

/* input + trailing icon button (password toggle) */
.input-affix { position: relative; }
.input-affix input { padding-right: 52px; }
.input-affix .affix-btn {
    position: absolute; top: 0; right: 0; height: 100%; width: 48px;
    display: flex; align-items: center; justify-content: center;
    background: none; border: none; cursor: pointer; color: var(--muted-fg);
    border-radius: 0 var(--r-sm) var(--r-sm) 0;
}
.input-affix .affix-btn:focus-visible { outline: 2px solid var(--ring); outline-offset: -2px; }

/* ---------------- buttons ---------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    min-height: 48px;
    border: 1.5px solid transparent;
    border-radius: 10px;
    padding: var(--sp-3) var(--sp-5);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    text-align: center;
    touch-action: manipulation;
    transition: filter .15s ease-out, transform .12s ease-out, background-color .15s ease-out;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: scale(.98); }
.btn:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
.btn-primary { background: var(--maroon); color: #fff; }
.btn-primary:focus-visible { outline-color: var(--maroon-dark); }
.btn-success { background: var(--success); color: #fff; }
.btn-outline { background: var(--surface); color: var(--maroon); border-color: var(--maroon); }
.btn-ghost { background: var(--surface); color: var(--muted-fg); border-color: var(--border); }
.btn-danger-outline { background: var(--surface); color: var(--danger); border-color: var(--danger); }
.btn-block { display: flex; width: 100%; }
.btn-lg { min-height: 56px; padding: var(--sp-4) var(--sp-5); font-size: 1.05rem; }
.btn-sm { min-height: 44px; padding: var(--sp-2) var(--sp-4); font-size: .9rem; font-weight: 500; }
.btn .icon { flex: none; }

/* inline SVG icons */
.icon { width: 20px; height: 20px; stroke: currentColor; stroke-width: 2;
        fill: none; stroke-linecap: round; stroke-linejoin: round; }
.icon-sm { width: 16px; height: 16px; }
.icon-lg { width: 28px; height: 28px; }

/* button spinner (loading state) */
.spinner {
    width: 20px; height: 20px; flex: none;
    border: 2.5px solid rgba(255, 255, 255, .35);
    border-top-color: #fff;
    border-radius: 50%;
    animation: mg-spin .7s linear infinite;
}
.spinner-dark { border-color: rgba(140, 21, 21, .25); border-top-color: var(--maroon); }
@keyframes mg-spin { to { transform: rotate(360deg); } }

/* ---------------- badges ---------------- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border-radius: var(--r-full);
    padding: var(--sp-2) var(--sp-4);
    font-weight: 700;
    font-size: .95rem;
}
.badge .icon { width: 18px; height: 18px; }
.badge-green { background: var(--success-tint); color: var(--success); }
.badge-red { background: var(--danger-tint); color: var(--danger); }
.badge-grey { background: #ece7de; color: #5c564e; }
.badge-sm { font-size: .78rem; padding: 2px 10px; font-weight: 600; }

.error { color: var(--danger); font-size: .9rem; }
.success { color: var(--success); font-size: .9rem; }

.alert {
    display: flex; gap: var(--sp-3); align-items: flex-start;
    border-radius: var(--r-md); padding: var(--sp-4);
    font-size: .95rem;
}
.alert .icon { flex: none; margin-top: 2px; }
.alert-danger { background: var(--danger-tint); color: #8f1f34; }

/* ---------------- survey ---------------- */
.q-section {
    font-family: var(--font-head);
    font-weight: 600;
    font-size: 1.02rem;
    color: var(--maroon);
    border-bottom: 2px solid var(--border);
    padding-bottom: var(--sp-2);
    margin: var(--sp-6) 0 var(--sp-4);
}
.q { margin-bottom: var(--sp-5); }
.q > label { font-weight: 600; line-height: 1.5; }

/* choice rows: whole row tappable, >=48px, selected shows check icon (not color-only) */
.q .opts { display: flex; flex-direction: column; gap: var(--sp-2); }
.q .opts label {
    display: flex; align-items: center; gap: var(--sp-3);
    margin: 0; font-weight: 400; cursor: pointer;
    min-height: 48px; padding: var(--sp-2) var(--sp-3);
    border: 1.5px solid var(--border); border-radius: var(--r-sm);
    background: var(--surface);
    transition: border-color .15s ease-out, background-color .15s ease-out;
    touch-action: manipulation;
}
.q .opts label:active { background: var(--maroon-tint); }
.q .opts label:focus-within { outline: 3px solid var(--ring); outline-offset: 1px; }
.q .opts input[type=radio], .q .opts input[type=checkbox] {
    flex: none; width: 20px; height: 20px; margin: 0;
    accent-color: var(--maroon); cursor: pointer;
}
.q .opts label:has(input:checked) {
    border-color: var(--maroon);
    background: var(--maroon-tint);
    font-weight: 600;
}
.q .opts .opt-check { display: none; margin-left: auto; color: var(--maroon); flex: none; }
.q .opts label:has(input:checked) .opt-check { display: block; }

/* highlight the question a validation error points at */
.q.q-missing > label { color: var(--danger); }
.q.q-missing .opts label { border-color: var(--danger); }

/* legacy numeric scale (kept for scale-type questions) */
.scale { display: flex; gap: var(--sp-2); }
.scale label { flex: 1; margin: 0; font-weight: 400; }
.scale input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.scale span {
    display: block; text-align: center; min-height: 48px; line-height: 48px;
    border: 1.5px solid var(--border); border-radius: var(--r-sm); cursor: pointer;
    transition: background-color .15s ease-out;
}
.scale input:checked + span { background: var(--maroon); border-color: var(--maroon); color: #fff; }
.scale input:focus-visible + span { outline: 3px solid var(--ring); outline-offset: 1px; }
.scale-ends { display: flex; justify-content: space-between; }

/* sticky survey progress */
.progress-bar {
    position: sticky; top: 0; z-index: var(--z-sticky);
    background: rgba(247, 244, 239, .95);
    backdrop-filter: blur(4px);
    margin: 0 calc(-1 * var(--sp-4));
    padding: var(--sp-2) var(--sp-4);
}
.progress-track {
    height: 6px; border-radius: var(--r-full); background: #e6dfd4; overflow: hidden;
}
.progress-fill {
    height: 100%; width: 0; border-radius: var(--r-full);
    background: var(--maroon);
    transition: width .25s ease-out;
}
.progress-text { font-size: .8rem; color: var(--muted-fg); text-align: right; margin-top: 2px; }

/* ---------------- QR ---------------- */
.qr-frame {
    width: min(320px, 80vw);
    aspect-ratio: 1;                 /* reserve space: no layout shift */
    margin: 0 auto;
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    background:
        linear-gradient(100deg, transparent 30%, rgba(255,255,255,.7) 50%, transparent 70%) #efe9df;
    background-size: 200% 100%;
    animation: mg-shimmer 1.2s linear infinite;
    overflow: hidden;
}
.qr-frame.loaded { animation: none; background: var(--surface); }
.qr-frame img { display: block; width: 100%; height: 100%; }
@keyframes mg-shimmer { to { background-position: -200% 0; } }

/* ---------------- header bar (staff/admin) ---------------- */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    gap: var(--sp-3); margin-bottom: var(--sp-4); flex-wrap: wrap;
}
.topbar h1 {
    margin: 0; color: var(--maroon); font-size: 1.35rem;
    display: flex; align-items: center; gap: var(--sp-2);
}
.topbar .mascot { width: 34px; height: 34px; }

/* video scanner */
video.scanner { width: 100%; aspect-ratio: 4 / 3; border-radius: var(--r-md); background: #141210; object-fit: cover; }

/* ---------------- tables (admin) ---------------- */
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted-fg); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
tr:hover td { background: #faf7f2; }
td.num { font-variant-numeric: tabular-nums; }
.table-scroll { max-height: 480px; overflow-y: auto; }
.table-scroll thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; box-shadow: 0 1px 0 var(--border); }

/* stat cards */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--sp-3); margin-bottom: var(--sp-4); }
.stat { border-radius: var(--r-md); padding: var(--sp-4); color: #fff; }
.stat .n { font-family: var(--font-head); font-size: 1.9rem; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .l { font-size: .82rem; opacity: .92; }
.stat-maroon { background: var(--maroon); }
.stat-blue { background: #2f6280; }
.stat-green { background: var(--success); }
.stat-orange { background: #a86413; }

/* tabs (admin) */
.tabs { display: flex; gap: var(--sp-1); border-bottom: 2px solid var(--border); margin-bottom: var(--sp-4); flex-wrap: wrap; }
.tabs button {
    border: none; background: none; font: inherit; font-family: var(--font-head); font-weight: 600;
    min-height: 44px; padding: var(--sp-2) var(--sp-4); cursor: pointer; color: var(--muted-fg);
    border-bottom: 3px solid transparent; margin-bottom: -2px;
    touch-action: manipulation;
    transition: color .15s ease-out, border-color .15s ease-out;
}
.tabs button:focus-visible { outline: 3px solid var(--ring); outline-offset: -3px; }
.tabs button.active { color: var(--maroon); border-bottom-color: var(--maroon); }

/* modal */
.modal-bg {
    position: fixed; inset: 0; background: rgba(20, 12, 8, .5);
    display: flex; align-items: center; justify-content: center;
    padding: var(--sp-4); z-index: var(--z-modal);
}
.modal {
    background: var(--surface); border-radius: var(--r-md); padding: var(--sp-5);
    max-width: 640px; width: 100%; max-height: 85vh; overflow-y: auto;
    box-shadow: var(--shadow-2);
}

/* fixed language toggle on graduate-facing pages */
.lang-fixed {
    position: fixed;
    top: calc(var(--sp-3) + env(safe-area-inset-top));
    right: var(--sp-3);
    z-index: var(--z-fixed);
}

.footer { text-align: center; color: var(--muted-fg); font-size: .8rem; padding: var(--sp-4) 0 var(--sp-6); }
.footer a { color: var(--muted-fg); min-height: 44px; display: inline-flex; align-items: center; padding: 0 var(--sp-3); }
.footer a:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; border-radius: var(--r-sm); }

a { color: var(--maroon); }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
    .qr-frame { animation: none; }
}
