:root {
  --bg: #f5f2f8;
  --surface: rgba(255, 255, 255, .88);
  --surface-solid: #ffffff;
  --ink: #1c1924;
  --muted: #777180;
  --line: rgba(37, 29, 53, .12);
  --accent: #6c4df6;
  --accent-dark: #5235d4;
  --accent-soft: #ece7ff;
  --danger: #bf3b4b;
  --success: #248b64;
  --shadow: 0 18px 54px rgba(38, 26, 62, .12);
  --radius: 24px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 15% 0%, rgba(124, 92, 255, .16), transparent 35%),
    radial-gradient(circle at 95% 12%, rgba(240, 93, 139, .12), transparent 32%),
    var(--bg);
}
button, input, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.dice-app { width: min(1440px, 100%); margin: 0 auto; padding: 18px 22px 26px; }
.dice-header {
  display: flex; align-items: center; justify-content: space-between; gap: 18px;
  padding: 10px 4px 18px;
}
.brand-block { display: flex; align-items: center; gap: 13px; min-width: 0; }
.school-logo-wrap {
  width: 48px; height: 48px; border-radius: 16px; display: grid; place-items: center; overflow: hidden;
  color: white; font-weight: 800; letter-spacing: -.05em;
  background: linear-gradient(145deg, #201b2d, #6c4df6); box-shadow: 0 10px 30px rgba(71, 51, 150, .25);
  flex: 0 0 auto;
}
.school-logo-wrap img { width: 100%; height: 100%; object-fit: contain; background: white; }
.brand-caption { margin: 0 0 2px; color: var(--muted); font-size: 12px; line-height: 1.2; }
.dice-header h1 { margin: 0; font-size: clamp(22px, 3vw, 34px); line-height: 1.05; letter-spacing: -.045em; }
.header-actions { display: flex; gap: 7px; }
.icon-button {
  width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 14px; background: rgba(255,255,255,.72);
  display: grid; place-items: center; font-size: 18px; transition: .2s ease;
}
.icon-button:hover { transform: translateY(-1px); background: white; }
.icon-button.is-off { opacity: .45; filter: grayscale(1); }
.icon-button.is-active { border-color: rgba(108,77,246,.45); background: var(--accent-soft); }

.mode-tabs {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; max-width: 440px; margin: 0 auto 18px;
  padding: 5px; border: 1px solid var(--line); border-radius: 18px; background: rgba(255,255,255,.65); backdrop-filter: blur(12px);
}
.mode-tab { border: 0; border-radius: 13px; padding: 12px 18px; background: transparent; color: var(--muted); font-weight: 700; }
.mode-tab.active { background: var(--surface-solid); color: var(--ink); box-shadow: 0 5px 18px rgba(38,26,62,.09); }

.room-entry { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; margin-bottom: 18px; }
.entry-card, .set-panel, .roll-area, .room-bar {
  border: 1px solid var(--line); background: var(--surface); backdrop-filter: blur(16px); box-shadow: var(--shadow);
}
.entry-card { padding: 22px; border-radius: var(--radius); }
.entry-card h2 { margin: 5px 0 8px; font-size: 23px; }
.entry-card p { color: var(--muted); line-height: 1.55; margin: 0 0 18px; }
.eyebrow { color: var(--accent); font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .13em; }
.inline-form { display: grid; grid-template-columns: 1fr 1.2fr auto; gap: 8px; }
.inline-form input, .modal-form input, .select-field select {
  width: 100%; border: 1px solid var(--line); border-radius: 13px; background: white; color: var(--ink); padding: 12px 13px; outline: none;
}
.inline-form input:focus, .modal-form input:focus, .select-field select:focus { border-color: rgba(108,77,246,.55); box-shadow: 0 0 0 3px rgba(108,77,246,.11); }

.primary-button, .secondary-button, .soft-button, .danger-button {
  border-radius: 14px; min-height: 44px; padding: 10px 16px; font-weight: 750; transition: .2s ease;
}
.primary-button { border: 0; color: white; background: linear-gradient(135deg, var(--accent), var(--accent-dark)); box-shadow: 0 10px 24px rgba(91,65,215,.25); }
.secondary-button { border: 1px solid rgba(108,77,246,.24); color: var(--accent-dark); background: var(--accent-soft); }
.soft-button { border: 1px solid var(--line); background: white; color: var(--ink); }
.danger-button { border: 1px solid rgba(191,59,75,.25); background: #fff2f3; color: var(--danger); }
.primary-button:hover, .secondary-button:hover, .soft-button:hover, .danger-button:hover { transform: translateY(-1px); }
button:disabled { cursor: not-allowed; opacity: .48; transform: none !important; }

.room-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 10px 14px; border-radius: 18px; margin-bottom: 14px;
}
.room-main-info { display: flex; align-items: center; gap: 9px; }
.room-main-info div { display: flex; flex-direction: column; }
.room-main-info small { color: var(--muted); font-size: 10px; }
.room-main-info strong { letter-spacing: .12em; }
.room-live-dot { width: 9px; height: 9px; border-radius: 50%; background: #33b779; box-shadow: 0 0 0 5px rgba(51,183,121,.13); }
.copy-button, .room-panel-toggle { border: 1px solid var(--line); background: white; border-radius: 11px; padding: 8px 11px; color: var(--ink); font-weight: 650; }
.room-status { color: var(--muted); font-size: 13px; text-align: center; }

.workspace { display: grid; grid-template-columns: 350px minmax(0, 1fr); gap: 16px; align-items: stretch; }
.set-panel { border-radius: var(--radius); padding: 19px; min-height: 610px; }
.panel-title-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.panel-title-row h2 { margin: 5px 0 0; font-size: 22px; }
.count-badge { background: var(--accent-soft); color: var(--accent-dark); border-radius: 999px; padding: 6px 10px; font-weight: 800; font-size: 12px; }
.select-field { display: grid; gap: 6px; font-size: 12px; color: var(--muted); margin: 18px 0 14px; }
.select-field select { color: var(--ink); }
.dice-list { display: grid; gap: 9px; max-height: 345px; overflow: auto; padding-right: 3px; }
.die-row {
  display: grid; grid-template-columns: 44px minmax(0,1fr) 74px 42px; align-items: center; gap: 8px;
  border: 1px solid var(--line); background: rgba(255,255,255,.78); border-radius: 15px; padding: 8px;
}
.die-token { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; font-weight: 850; color: white; text-shadow: 0 1px 3px rgba(0,0,0,.25); }
.die-row select { min-width: 0; border: 1px solid var(--line); background: white; border-radius: 10px; padding: 8px; }
.die-row input[type="color"] { width: 100%; height: 34px; border: 0; padding: 2px; background: transparent; }
.remove-die { width: 34px; height: 34px; border-radius: 10px; border: 0; background: #f7f3f5; color: var(--danger); font-size: 17px; }
.add-die-block { margin-top: 15px; padding-top: 14px; border-top: 1px solid var(--line); }
.add-die-block p { margin: 0 0 9px; color: var(--muted); font-size: 12px; }
.side-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.side-button { border: 1px solid var(--line); border-radius: 10px; background: white; min-width: 43px; padding: 8px 9px; font-weight: 750; }
.side-button:hover { border-color: rgba(108,77,246,.45); color: var(--accent-dark); }
.set-actions { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 7px; margin-top: 15px; }
.set-actions .soft-button:last-child { grid-column: 1 / -1; }

.roll-area { position: relative; border-radius: var(--radius); padding: 18px; min-height: 610px; display: flex; flex-direction: column; }
.turn-banner, .roller-banner {
  border-radius: 13px; padding: 10px 13px; margin-bottom: 9px; text-align: center; font-weight: 750; font-size: 13px;
}
.turn-banner { color: #5c4211; background: #fff7d9; border: 1px solid #f2dea0; }
.roller-banner { color: var(--accent-dark); background: var(--accent-soft); border: 1px solid rgba(108,77,246,.18); }
.dice-stage {
  position: relative; flex: 1; min-height: 390px; width: 100%; border: 0; border-radius: 22px; overflow: hidden; padding: 18px;
  background:
    radial-gradient(circle at 50% 43%, rgba(255,255,255,.95), rgba(255,255,255,.58) 45%, rgba(232,226,242,.48) 78%),
    linear-gradient(145deg, #f8f6fb, #ece7f3);
  box-shadow: inset 0 0 0 1px rgba(37,29,53,.08), inset 0 -28px 50px rgba(65,48,91,.05);
  touch-action: pan-y;
}
.dice-stage::after { content: ''; position: absolute; left: 10%; right: 10%; bottom: 17%; height: 14%; border-radius: 50%; background: rgba(45,32,63,.11); filter: blur(25px); }
.stage-placeholder { position: absolute; inset: 0; z-index: 2; display: grid; place-content: center; text-align: center; color: var(--muted); gap: 5px; }
.placeholder-icon { font-size: 48px; color: rgba(108,77,246,.45); }
.stage-placeholder strong { color: var(--ink); font-size: 19px; }
.dice-visuals { position: relative; z-index: 3; min-height: 330px; height: 100%; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; align-content: center; gap: clamp(10px, 2.4vw, 28px); }
.die-slot { position: relative; width: clamp(78px, 12vw, 132px); padding-bottom: 18px; display: grid; place-items: center; }
.die-visual { position: relative; width: 100%; aspect-ratio: 1; display: grid; place-items: center; filter: drop-shadow(0 14px 11px rgba(33,25,48,.18)); transform-origin: center; }
.die-visual svg { width: 100%; height: 100%; overflow: visible; }
.die-visual.is-rolling { animation: tumble 520ms linear infinite; }
.die-visual.is-final { animation: settle .46s cubic-bezier(.2,1.6,.45,1); }
.die-label { position: absolute; z-index: 2; bottom: 0; right: -3px; background: rgba(25,21,34,.85); color: white; border-radius: 999px; font-size: 10px; padding: 4px 7px; letter-spacing: .04em; pointer-events: none; }
.swipe-hint { position: absolute; z-index: 4; left: 50%; bottom: 14px; transform: translateX(-50%); width: max-content; max-width: 90%; color: var(--muted); font-size: 11px; background: rgba(255,255,255,.72); border-radius: 999px; padding: 7px 11px; }
@keyframes tumble { 0% { transform: translateY(0) rotate(0deg) scale(1); } 30% { transform: translateY(-13px) rotate(113deg) scale(1.04); } 65% { transform: translateY(7px) rotate(245deg) scale(.97); } 100% { transform: translateY(0) rotate(360deg) scale(1); } }
@keyframes settle { 0% { transform: translateY(-18px) scale(.88) rotate(-8deg); } 70% { transform: translateY(4px) scale(1.06) rotate(2deg); } 100% { transform: none; } }

.result-panel { display: flex; align-items: center; justify-content: center; gap: 16px; margin: 12px 0 5px; min-height: 52px; }
.result-values { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.result-pill { border: 1px solid var(--line); background: white; border-radius: 999px; padding: 7px 11px; font-size: 12px; }
.result-pill strong { font-size: 15px; margin-left: 4px; }
.result-total { display: flex; align-items: baseline; gap: 8px; padding-left: 15px; border-left: 1px solid var(--line); }
.result-total span { color: var(--muted); font-size: 12px; }
.result-total strong { font-size: 29px; letter-spacing: -.04em; }
.roll-button {
  width: min(100%, 430px); align-self: center; border: 0; border-radius: 18px; min-height: 62px; margin-top: 10px;
  color: white; background: linear-gradient(135deg, var(--accent), #4c2fd1); font-size: 18px; font-weight: 850;
  box-shadow: 0 14px 34px rgba(83,53,208,.29); display: flex; align-items: center; justify-content: center; gap: 10px;
}
.roll-button:not(:disabled):hover { transform: translateY(-2px); }
.roll-button-icon { font-size: 24px; }
.roll-button.is-rolling .roll-button-icon { animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.after-roll-actions { min-height: 34px; display: flex; justify-content: center; flex-wrap: wrap; gap: 18px; margin-top: 9px; }
.text-action { border: 0; background: transparent; color: var(--accent-dark); font-weight: 700; padding: 7px; }
.danger-text { color: var(--danger); }

.dice-footer { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 11px; padding: 15px 5px 0; }

.room-drawer {
  position: fixed; z-index: 50; top: 0; right: 0; bottom: 0; width: min(420px, 94vw); padding: 22px;
  background: rgba(250,248,252,.97); backdrop-filter: blur(22px); box-shadow: -16px 0 55px rgba(35,27,48,.2); overflow: auto;
}
.drawer-head, .drawer-section-head { display: flex; justify-content: space-between; align-items: center; }
.drawer-head h2 { margin: 4px 0 0; letter-spacing: .14em; }
.modal-close { width: 38px; height: 38px; border: 1px solid var(--line); background: white; border-radius: 12px; font-size: 22px; }
.host-controls { display: grid; gap: 8px; padding: 17px 0; border-bottom: 1px solid var(--line); }
.control-row { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.drawer-section { padding: 18px 0; }
.drawer-section h3 { margin: 0; }
.drawer-section-head span { background: var(--accent-soft); color: var(--accent-dark); border-radius: 999px; padding: 4px 9px; font-weight: 800; }
.member-list { display: grid; gap: 8px; margin-top: 12px; }
.member-item { display: grid; grid-template-columns: 10px minmax(0,1fr) auto; gap: 9px; align-items: center; padding: 10px; border: 1px solid var(--line); background: white; border-radius: 14px; }
.member-online { width: 8px; height: 8px; border-radius: 50%; background: #b5b0b8; }
.member-online.online { background: #31aa74; }
.member-name { min-width: 0; }
.member-name strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-name small { color: var(--muted); }
.member-actions { display: flex; gap: 4px; }
.member-actions button { border: 0; border-radius: 9px; padding: 6px 8px; font-size: 11px; background: var(--accent-soft); color: var(--accent-dark); }
.member-actions button.remove { color: var(--danger); background: #fff1f2; }
.member-item.current { border-color: rgba(108,77,246,.4); box-shadow: 0 0 0 3px rgba(108,77,246,.08); }
.room-rules-summary { color: var(--muted); line-height: 1.55; font-size: 13px; }

.modal-layer {
  position: fixed; z-index: 70; inset: 0; display: grid; place-items: center; padding: 18px; overflow: auto;
  background: rgba(23,19,32,.55); backdrop-filter: blur(8px);
}
.modal-card { position: relative; width: min(600px, 100%); max-height: calc(100dvh - 36px); overflow: auto; background: #fbfafd; border-radius: 25px; padding: 25px; box-shadow: 0 26px 90px rgba(20,14,29,.35); }
.modal-card > .modal-close { position: absolute; top: 15px; right: 15px; }
.modal-card h2 { margin: 6px 45px 20px 0; font-size: 27px; letter-spacing: -.035em; }
.compact-modal { width: min(440px, 100%); }
.center-modal { text-align: center; }
.modal-form { display: grid; gap: 14px; }
.modal-form > label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.modal-form fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 7px; }
.modal-form legend { font-size: 12px; color: var(--muted); margin-bottom: 7px; }
.choice-card { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); background: white; border-radius: 14px; padding: 12px; cursor: pointer; }
.choice-card:has(input:checked) { border-color: rgba(108,77,246,.45); background: var(--accent-soft); }
.choice-card input { width: auto; margin-top: 3px; }
.choice-card span { display: grid; gap: 3px; }
.choice-card small { color: var(--muted); }
.form-message { border-radius: 12px; padding: 10px 12px; color: var(--success); background: #ecf9f3; }
.form-message.error { color: var(--danger); background: #fff0f1; }
.qr-image { width: min(280px, 75vw); aspect-ratio: 1; margin: 0 auto 12px; border-radius: 16px; background: white; }
.large-code { display: block; font-size: 25px; letter-spacing: .18em; margin-bottom: 14px; }
.favorite-list { display: grid; gap: 9px; }
.favorite-item { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 10px; align-items: center; border: 1px solid var(--line); background: white; border-radius: 15px; padding: 12px; }
.favorite-item p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.favorite-actions { display: flex; gap: 6px; }
.favorite-actions button { border: 1px solid var(--line); border-radius: 9px; padding: 7px 9px; background: white; }
.games-list { display: grid; gap: 9px; }
.games-list a { text-decoration: none; color: var(--ink); display: grid; gap: 3px; padding: 15px; border: 1px solid var(--line); background: white; border-radius: 15px; }
.games-list a:hover { border-color: rgba(108,77,246,.4); }
.games-list small { color: var(--muted); }
.toast { position: fixed; z-index: 100; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: #211d2b; color: white; padding: 11px 16px; border-radius: 999px; box-shadow: 0 12px 35px rgba(0,0,0,.25); font-size: 13px; max-width: 90vw; text-align: center; }

@media (max-width: 900px) {
  .dice-app { padding: 12px 12px 22px; }
  .workspace { grid-template-columns: 1fr; }
  .set-panel { min-height: 0; order: 2; }
  .roll-area { min-height: 590px; order: 1; }
  .dice-list { max-height: none; }
  .room-entry { grid-template-columns: 1fr; }
  .room-status { display: none; }
}

@media (max-width: 600px) {
  body { background: var(--bg); }
  .dice-header { padding: 6px 2px 12px; }
  .school-logo-wrap { width: 42px; height: 42px; border-radius: 13px; }
  .brand-caption { font-size: 10px; max-width: 190px; }
  .dice-header h1 { font-size: 22px; }
  .header-actions { gap: 4px; }
  .icon-button { width: 36px; height: 36px; border-radius: 11px; font-size: 15px; }
  .mode-tabs { max-width: none; margin-bottom: 11px; }
  .mode-tab { padding: 10px 8px; font-size: 13px; }
  .entry-card { padding: 17px; }
  .inline-form { grid-template-columns: 1fr; }
  .room-bar { flex-wrap: wrap; padding: 9px 10px; }
  .room-panel-toggle { margin-left: auto; }
  .workspace { gap: 10px; }
  .roll-area { padding: 10px; border-radius: 19px; min-height: calc(100dvh - 210px); }
  .dice-stage { min-height: 330px; border-radius: 17px; padding: 10px; }
  .dice-visuals { min-height: 290px; gap: 7px; }
  .die-visual { width: clamp(66px, 24vw, 105px); }
  .swipe-hint { font-size: 9px; bottom: 8px; }
  .result-panel { flex-direction: column; gap: 5px; }
  .result-total { border-left: 0; padding-left: 0; }
  .roll-button { min-height: 57px; border-radius: 16px; }
  .set-panel { padding: 15px; border-radius: 19px; }
  .die-row { grid-template-columns: 40px minmax(0,1fr) 65px 36px; gap: 5px; }
  .set-actions { grid-template-columns: 1fr; }
  .set-actions .soft-button:last-child { grid-column: auto; }
  .dice-footer { flex-direction: column; text-align: center; gap: 4px; }
  .modal-card { padding: 20px; border-radius: 20px; }
  .modal-card h2 { font-size: 23px; }
}

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