:root {
  color-scheme: dark;
  --ink: #f6eee0;
  --paper: #08090c;
  --panel: #111217;
  --panel-raised: #1b1d24;
  --line: #38313a;
  --muted: #a99da4;
  --accent: #e0a83d;
  --accent-strong: #a9691c;
  --accent-soft: #352415;
  --warm: #f6d77d;
  --success: #69d1a0;
  --danger: #ef5d6c;
  --ruby: #a9283c;
  --ruby-dark: #4a121d;
  --shadow: rgba(0, 0, 0, .78);
}

* { box-sizing: border-box; }
html { min-width: 320px; scroll-behavior: smooth; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    radial-gradient(circle at 50% -10%, rgba(189, 38, 61, .2), transparent 36%),
    linear-gradient(rgba(242, 184, 75, .035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(242, 184, 75, .035) 1px, transparent 1px);
  background-size: auto, 34px 34px, 34px 34px;
  font-family: "Noto Sans Thai", sans-serif;
}
body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  content: "";
  background: linear-gradient(180deg, rgba(16, 7, 9, .12), rgba(16, 7, 9, .94) 88%);
}
body::after {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  content: "";
  opacity: .18;
  background-image: radial-gradient(rgba(255, 224, 138, .55) .7px, transparent .7px);
  background-size: 7px 7px;
  mix-blend-mode: screen;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .55; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--warm);
  outline-offset: 2px;
}

.topbar {
  min-height: 76px;
  padding: 0 max(20px, calc((100vw - 1160px) / 2));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid #7a3c2c;
  background: rgba(17, 6, 9, .95);
  box-shadow: 0 5px 0 rgba(242, 184, 75, .08), 0 12px 34px rgba(0, 0, 0, .48);
}
.brand {
  color: var(--accent);
  text-decoration: none;
  font: 700 17px "IBM Plex Mono", monospace;
  letter-spacing: 1.5px;
  text-shadow: 0 0 8px rgba(242, 184, 75, .75), 0 0 22px rgba(189, 38, 61, .45);
}
.brand::before { content: "\2605  "; color: #fff1ad; }
.account-actions { display: flex; align-items: center; gap: 10px; }
.coin-display {
  min-height: 38px;
  padding: 8px 13px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid #a96d26;
  border-radius: 999px;
  background: #2e190d;
  color: var(--warm);
  font: 700 13px "IBM Plex Mono", monospace;
  box-shadow: inset 0 0 14px rgba(255, 224, 138, .08), 0 0 14px rgba(242, 184, 75, .08);
}
.coin-display::before { content: "\25C9"; color: var(--accent); font-size: 16px; }
.header-button, .outline-button, .primary-button {
  min-height: 38px;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 3px;
  font-weight: 700;
  font-size: 13px;
  transition: color .2s ease, background .2s ease, border-color .2s ease, transform .2s ease, box-shadow .2s ease;
}
.header-button, .outline-button { color: var(--ink); background: rgba(40, 17, 23, .82); }
.header-button:hover, .outline-button:hover { color: #1c080b; background: var(--warm); border-color: var(--warm); box-shadow: 0 0 16px rgba(255, 224, 138, .25); transform: translateY(-1px); }
.primary-button { color: #261008; background: var(--accent); border-color: var(--accent); box-shadow: 0 0 18px rgba(242, 184, 75, .2); }
.primary-button:hover { background: #fff0a9; border-color: #fff0a9; box-shadow: 0 0 22px rgba(255, 224, 138, .35); transform: translateY(-1px); }

.machine-page {
  width: min(800px, calc(100% - 40px));
  min-height: calc(100vh - 76px);
  margin: auto;
  padding: 64px 0 55px;
  display: grid;
  align-content: center;
  gap: 24px;
}
.machine-wrap { width: min(100%, 520px); margin: auto; text-align: center; }
.machine {
  position: relative;
  overflow: hidden;
  border: 4px solid #8f5125;
  border-radius: 8px;
  background: #0b0507;
  box-shadow: 0 20px 55px var(--shadow), 0 0 0 2px #3b151b, 0 0 28px rgba(189, 38, 61, .2), inset 0 0 0 1px rgba(255, 235, 156, .22);
}
.machine-top, .machine-bottom {
  min-height: 42px;
  padding: 0 17px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #ffe6b0;
  font: 700 11px "IBM Plex Mono", monospace;
  letter-spacing: .7px;
}
.machine-top { padding-top: 4px; color: var(--accent); background: #260d14; }
.machine-top span { min-width: 0; overflow-wrap: anywhere; }
.machine-top span:first-child { flex: 1 1 auto; }
.machine-top span:last-child { flex: 0 1 auto; text-align: right; }
.machine-bottom { color: #e6bfc0; background: #180910; }
.machine-bottom span { min-width: 0; overflow-wrap: anywhere; }
.machine-top span:last-child { color: var(--warm); }
.reel-frame {
  position: relative;
  padding: 16px;
  background: #42121d;
  border-block: 1px solid #a4512d;
  box-shadow: inset 0 8px 18px rgba(0, 0, 0, .48), inset 0 -8px 18px rgba(0, 0, 0, .48);
}
.reel {
  height: 235px;
  overflow: hidden;
  padding: 0;
  border: 4px solid #b87b2b;
  border-radius: 4px;
  background: #171119;
  color: #f6d77d;
  font-size: 96px;
  box-shadow: inset 0 0 28px rgba(0, 0, 0, .72), inset 0 0 0 1px rgba(255, 255, 255, .06), 0 0 18px rgba(224, 168, 61, .14);
}
.reel-track {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
}
.reel-item {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  padding: 14px;
  flex: 0 0 100%;
}
.reel-item img { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; object-fit: contain; object-position: center; }
.reel-item:not(:has(img)) { color: #f6d77d; font-size: 96px; text-shadow: 0 0 14px rgba(246, 215, 125, .55); }
.reel.is-processing { border-color: #fff0ad; box-shadow: inset 0 0 28px rgba(0, 0, 0, .72), inset 0 0 0 1px rgba(255, 255, 255, .06), 0 0 28px rgba(255, 224, 138, .52); }
.reel.is-processing .reel-item { color: #fff0ad; animation: revealPulse .68s ease-in-out infinite alternate; }
.spin-button.is-processing { color: #fff0ad; background: #6e241f; border-color: #efb052; box-shadow: 0 5px 0 #441116, 0 0 30px rgba(239, 93, 108, .38); }
@keyframes revealPulse { from { opacity: .38; transform: scale(.92); } to { opacity: 1; transform: scale(1); } }
.spin-modes { display: flex; justify-content: center; margin-top: 20px; border: 1px solid #8c4b28; border-radius: 4px; background: #1b0a10; box-shadow: 0 5px 18px rgba(0, 0, 0, .28); }
.spin-mode { flex: 1; min-height: 52px; padding: 7px 12px; border: 0; color: var(--muted); background: transparent; font: 700 15px "IBM Plex Mono", monospace; }
.spin-mode + .spin-mode { border-left: 1px solid #5d2730; }
.spin-mode small { display: block; margin-top: 2px; color: #a46d6f; font: 500 10px "Noto Sans Thai", sans-serif; }
.spin-mode.active { color: #2b120c; background: var(--accent); box-shadow: inset 0 -3px 0 #a95e19; }
.spin-mode.active small { color: #74451a; }
.spin-button {
  width: 100%;
  min-height: 64px;
  margin-top: 13px;
  border: 2px solid #fff0ad;
  border-radius: 4px;
  color: #32100f;
  background: var(--accent);
  font-weight: 800;
  font-size: 18px;
  box-shadow: 0 5px 0 #8b451c, 0 0 26px rgba(242, 184, 75, .25);
  transition: filter .2s ease, transform .2s ease, box-shadow .2s ease;
}
.spin-button small { display: block; margin-top: 2px; color: #71451a; font: 600 11px "IBM Plex Mono", monospace; }
.spin-button:hover { filter: brightness(1.1); box-shadow: 0 5px 0 #8b451c, 0 0 34px rgba(255, 224, 138, .45); transform: translateY(-2px); }
.spin-button:active { box-shadow: 0 2px 0 #8b451c; transform: translateY(3px); }

.claims-panel {
  width: min(100%, 800px);
  padding: 22px 25px;
  border: 1px solid #443943;
  border-left: 4px solid var(--accent);
  background: rgba(17, 18, 23, .96);
  box-shadow: 0 12px 30px rgba(0, 0, 0, .44), inset 0 0 22px rgba(255, 255, 255, .02);
}
.result-backdrop {
  position: fixed;
  inset: 0;
  z-index: 7;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  background: rgba(5, 2, 4, .78);
  backdrop-filter: blur(3px);
  transition: opacity .22s ease, visibility .22s ease;
}
.result-backdrop.open { opacity: 1; visibility: visible; pointer-events: auto; }
.result-panel {
  position: fixed;
  inset: 50% auto auto 50%;
  z-index: 8;
  width: min(620px, calc(100% - 40px));
  padding: 28px;
  display: grid;
  grid-template-columns: 100px minmax(0, 1fr);
  gap: 18px 22px;
  border: 1px solid #b77a2d;
  border-left: 5px solid var(--accent);
  background: #151016;
  box-shadow: 0 0 0 1px rgba(255, 235, 156, .13), 0 26px 70px rgba(0, 0, 0, .72), inset 0 0 40px rgba(224, 168, 61, .045);
  transform: translate(-50%, -48%) scale(.96);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity .2s ease, transform .25s ease, visibility .2s ease;
}
.result-panel.open { opacity: 1; visibility: visible; pointer-events: auto; transform: translate(-50%, -50%) scale(1); }
.result-close { position: absolute; top: 8px; right: 8px; }
.result-visual { width: 100px; height: 100px; display: grid; place-items: center; border: 1px solid #8d522a; background: #251015; box-shadow: inset 0 0 22px rgba(0, 0, 0, .6), 0 0 22px rgba(224, 168, 61, .12); opacity: 0; transform: scale(.76); }
.result-icon { color: var(--warm); font-size: 58px; text-align: center; text-shadow: 0 0 18px rgba(255, 224, 138, .65); }
.result-image { width: 84px; height: 84px; object-fit: contain; }
.result-copy, .result-details, .result-footer { opacity: 0; transform: translateY(8px); }
.result-panel.open .result-visual { animation: resultReveal .32s cubic-bezier(.2, .9, .25, 1.15) .08s forwards; }
.result-panel.open .result-copy { animation: resultReveal .28s ease .18s forwards; }
.result-panel.open .result-details { animation: resultReveal .28s ease .29s forwards; }
.result-panel.open .result-footer { animation: resultReveal .28s ease .38s forwards; }
@keyframes resultReveal { to { opacity: 1; transform: translateY(0) scale(1); } }
.result-copy { min-width: 0; }
.result-panel p, .claim-row p { margin: 0; color: var(--muted); font-size: 13px; }
.result-panel h1 { margin: 4px 0 7px; color: #fff5cc; font-size: 23px; overflow-wrap: anywhere; }
.result-panel #resultTier, .eyebrow, .claim-kind { color: var(--accent); font: 700 11px "IBM Plex Mono", monospace; letter-spacing: .5px; }
.result-details { grid-column: 1 / -1; display: grid; grid-template-columns: minmax(100px, .65fr) minmax(0, 1.35fr); margin: 0; border-top: 1px solid #433039; border-bottom: 1px solid #433039; }
.result-details dt, .result-details dd { min-width: 0; margin: 0; padding: 9px 0; font: 500 12px "IBM Plex Mono", monospace; overflow-wrap: anywhere; }
.result-details dt { color: #aa9090; border-bottom: 1px solid rgba(67, 48, 57, .62); }
.result-details dd { color: #f6d77d; text-align: right; border-bottom: 1px solid rgba(67, 48, 57, .62); }
.result-details dt:nth-last-of-type(1), .result-details dd:last-child { border-bottom: 0; }
.result-footer { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 14px; color: #96776b; font: 600 10px "IBM Plex Mono", monospace; letter-spacing: .7px; }
.result-confirm { min-height: 35px; padding: 7px 16px; border: 1px solid var(--accent); border-radius: 3px; color: #281108; background: var(--accent); font-weight: 800; }
.result-confirm:hover { background: #fff0a9; border-color: #fff0a9; }
.claims-panel h2 { margin: 2px 0 14px; font-size: 18px; }
.eyebrow { margin: 0; }
.claims-list { display: grid; gap: 8px; }
.claim-row { padding: 13px; display: flex; align-items: center; justify-content: space-between; gap: 15px; border: 1px solid var(--line); background: rgba(40, 17, 23, .8); }
.claim-name { display: block; margin: 2px 0; }

.modal-backdrop { position: fixed; inset: 0; z-index: 10; display: none; align-items: center; justify-content: center; padding: 20px; background: rgba(8, 2, 4, .84); backdrop-filter: blur(5px); }
.modal-backdrop.open { display: flex; }
.admin-modal { width: min(980px, 100%); max-height: calc(100vh - 40px); overflow: auto; border: 1px solid #a15a32; border-radius: 4px; background: var(--panel); box-shadow: 8px 8px 0 #050203, 0 20px 60px rgba(0, 0, 0, .65), 0 0 30px rgba(189, 38, 61, .14); }
.admin-header, .admin-footer { display: flex; align-items: center; justify-content: space-between; padding: 17px 20px; border-bottom: 1px solid var(--line); }
.admin-header h2 { margin: 0; color: var(--accent); font-size: 21px; }
.admin-footer { border-top: 1px solid var(--line); border-bottom: 0; }
.admin-footer > :first-child { margin-right: auto; }
.icon-button { min-width: 32px; min-height: 32px; border: 0; color: var(--muted); background: transparent; font-size: 18px; line-height: 1; }
.icon-button:hover { color: var(--danger); }
.admin-content { display: grid; grid-template-columns: 175px 1fr; min-height: 410px; }
.admin-side { display: flex; flex-direction: column; gap: 4px; padding: 12px; border-right: 1px solid var(--line); background: #12070b; }
.admin-tab { padding: 11px 12px; border: 1px solid transparent; color: var(--muted); background: transparent; text-align: left; font-weight: 700; }
.admin-tab:hover { color: var(--ink); background: var(--panel-raised); }
.admin-tab.active { color: var(--accent); border-color: #8a4b27; background: var(--accent-soft); }
.admin-tab.danger { margin-top: auto; color: var(--danger); }
.admin-main { min-width: 0; padding: 22px; }
.tab-content { display: none; }
.tab-content.active { display: block; }
.section-heading { display: flex; align-items: start; justify-content: space-between; gap: 14px; margin-bottom: 18px; }
.section-heading h3 { margin: 0 0 4px; font-size: 18px; }
.section-heading p { margin: 0; color: var(--muted); font-size: 12px; }
.reward-list { display: grid; gap: 12px; }
.reward-editor { padding: 14px; border: 1px solid var(--line); background: #18191f; }
.reward-editor-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.reward-index { color: var(--accent); font: 600 12px "IBM Plex Mono", monospace; }
.reward-fields { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 12px; }
.reward-fields label, .reward-payload label, .coin-form label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 600; }
.reward-fields input, .reward-fields select, .reward-payload input, .reward-payload select, .reward-payload textarea, .coin-form input { width: 100%; min-width: 0; padding: 9px 10px; border: 1px solid var(--line); color: var(--ink); background: #10070a; font-size: 13px; }
.reward-fields input:focus, .reward-fields select:focus, .reward-payload input:focus, .reward-payload select:focus, .reward-payload textarea:focus, .coin-form input:focus { border-color: var(--accent); outline: 0; box-shadow: 0 0 0 2px rgba(242, 184, 75, .12); }
.reward-payload { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.reward-payload textarea { min-height: 80px; resize: vertical; }
.reward-payload label:has(textarea) { grid-column: 1 / -1; }
.payload-note, .payload-check { color: var(--muted); font-size: 11px; }
.payload-check input { width: auto; }
.punishment-options { display: grid; gap: 8px; grid-column: 1 / -1; }
.reward-preview { min-height: 100px; display: grid; place-items: center; border: 1px dashed #9a5730; background: #12070b; }
.reward-preview img { width: 88px; height: 88px; object-fit: contain; }
.reward-icon-preview { color: var(--warm); font-size: 42px; }
.admin-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 18px; color: var(--muted); font-size: 12px; }
.error-text { color: var(--danger); }
.coin-form { display: grid; gap: 14px; max-width: 520px; }
.coin-actions { display: flex; gap: 10px; }
.coin-result { margin-top: 18px; padding: 12px; border: 1px solid var(--line); color: var(--muted); background: #10070a; font-size: 13px; }
.toast { position: fixed; right: 20px; bottom: 20px; z-index: 20; max-width: min(360px, calc(100% - 40px)); padding: 12px 16px; border: 1px solid var(--accent); color: var(--accent); background: #32170f; box-shadow: 0 10px 30px rgba(0, 0, 0, .5); opacity: 0; pointer-events: none; transform: translateY(12px); transition: opacity .2s ease, transform .2s ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { color: #ffdce0; border-color: var(--danger); background: #321017; }

@media (max-width: 650px) {
  .topbar { padding: 10px 15px; align-items: flex-start; }
  .brand { font-size: 12px; padding-top: 7px; }
  .account-actions { flex-wrap: wrap; justify-content: flex-end; gap: 5px; }
  .coin-display { padding: 7px 9px; font-size: 11px; }
  .header-button { padding: 6px 8px; font-size: 11px; }
  .machine-page { width: min(calc(100% - 28px), 560px); padding-top: 42px; }
  .machine { border-width: 3px; }
  .reel { height: 190px; }
  .reel-item { height: 190px; flex-basis: 190px; }
  .result-panel { padding: 17px; gap: 12px; }
  .result-panel > div { min-width: 0; }
  .result-icon { flex-basis: 40px; font-size: 36px; }
  .result-image { width: 56px; height: 56px; flex-basis: 56px; }
  .result-panel h1 { font-size: 17px; overflow-wrap: anywhere; }
  .result-panel p { overflow-wrap: anywhere; }
  .claim-row { align-items: start; flex-direction: column; }
  .admin-content { grid-template-columns: 1fr; }
  .admin-side { flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 1px solid var(--line); }
  .admin-tab.danger { margin: 0 0 0 auto; }
  .admin-main { padding: 16px; }
  .reward-fields { grid-template-columns: 1fr 1fr; }
  .reward-fields label:first-child { grid-column: 1 / 3; }
  .reward-payload { grid-template-columns: 1fr; }
  .admin-footer { padding: 14px; }
  .admin-footer > :first-child { display: none; }
}

@media (max-width: 420px) {
  .topbar {
    min-height: 0;
    padding: 10px 12px;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .brand { padding-top: 0; text-align: center; }
  .account-actions {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    justify-content: stretch;
  }
  .coin-display { min-width: 0; justify-content: center; }
  .header-button { min-width: 0; width: 100%; }
  #adminTrigger { grid-column: 1 / -1; }
  .machine-page {
    width: calc(100% - 20px);
    min-height: auto;
    padding: 28px 0 32px;
    gap: 18px;
  }
  .machine-top, .machine-bottom { min-height: 38px; padding-inline: 10px; font-size: 9px; }
  .reel-frame { padding: 10px; }
  .reel { height: 170px; font-size: 72px; }
  .reel-item { padding: 8px; }
  .reel-item:not(:has(img)) { font-size: 72px; }
  .spin-modes { margin-top: 14px; }
  .spin-mode { min-height: 48px; padding-inline: 6px; font-size: 13px; }
  .spin-mode small { font-size: 9px; }
  .spin-button { min-height: 58px; font-size: 16px; }
  .result-panel { width: min(620px, calc(100% - 28px)); grid-template-columns: 72px minmax(0, 1fr); padding: 20px 16px 16px; gap: 14px; }
  .result-visual { width: 72px; height: 72px; }
  .result-icon { font-size: 42px; }
  .result-image { width: 60px; height: 60px; }
  .result-panel h1 { font-size: 18px; }
  .result-panel p { font-size: 12px; }
  .result-details { grid-template-columns: 94px minmax(0, 1fr); }
  .result-details dt, .result-details dd { padding-block: 8px; font-size: 10px; }
  .result-footer { font-size: 9px; }
  .modal-backdrop { padding: 8px; }
  .admin-modal { max-height: calc(100vh - 16px); }
  .admin-header, .admin-footer { padding: 12px; }
  .admin-header h2 { font-size: 18px; }
  .admin-main { padding: 12px; }
  .section-heading { align-items: stretch; flex-direction: column; gap: 10px; }
  .section-heading .outline-button { width: 100%; }
  .reward-fields { grid-template-columns: 1fr; }
  .reward-fields label:first-child { grid-column: auto; }
  .reward-payload { gap: 10px; }
  .admin-actions, .coin-actions { align-items: stretch; flex-direction: column; }
  .admin-actions button, .coin-actions button { width: 100%; }
}
