/* ========================================================================
   Chaos Bag — Mobile-First Dark Theme
   ======================================================================== */

/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: #1a1a2e;
  color: #e0e0e0;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  color: #ffffff;
}

/* === Layout === */
body {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

/* === View Switching === */
.view {
  display: none;
}
.view.active {
  display: block;
}

/* === Utility === */
.hidden {
  display: none !important;
}

/* === Connection Screen === */
#view-connect {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}
#view-connect.active {
  display: flex;
}
.connect-card {
  background: #16213e;
  border-radius: 12px;
  padding: 32px 24px;
  width: 100%;
  text-align: center;
}
.connect-card h1 {
  font-size: 2rem;
  margin-bottom: 4px;
}
.connect-card .subtitle {
  font-size: 0.9rem;
  color: #888;
  margin-bottom: 24px;
}
.form-group {
  margin-bottom: 12px;
  text-align: left;
}
.form-group label {
  display: block;
  font-size: 0.85rem;
  color: #aaa;
  margin-bottom: 4px;
}
.form-group input {
  width: 100%;
  padding: 12px 14px;
  background: #0f3460;
  border: 1px solid #1a4a7a;
  border-radius: 8px;
  color: #e0e0e0;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus {
  border-color: #e94560;
}
.form-group input::placeholder {
  color: #666;
}
.error-msg {
  margin-top: 12px;
  padding: 10px;
  background: #e94560;
  color: #fff;
  border-radius: 8px;
  font-size: 0.9rem;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s, transform 0.1s;
  text-decoration: none;
  width: 100%;
}
.btn:active {
  transform: scale(0.97);
}
.btn:disabled {
  background: #555 !important;
  color: #888 !important;
  cursor: not-allowed;
  opacity: 0.6;
}
.btn-primary {
  background: #e94560;
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: #d63850;
}
.btn-secondary {
  background: #0f3460;
  color: #e0e0e0;
}
.btn-secondary:hover:not(:disabled) {
  background: #1a4a7a;
}
.btn-danger {
  background: #533483;
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  background: #6a41a0;
}
.btn-large {
  padding: 14px 20px;
  font-size: 1.15rem;
}
.btn-sm {
  min-height: 36px;
  padding: 6px 14px;
  font-size: 0.85rem;
  width: auto;
}
.btn-leave {
  background: transparent;
  color: #e94560;
  border: 1px solid #e94560;
}
.btn-leave:hover:not(:disabled) {
  background: #e94560;
  color: #fff;
}

/* === Divider === */
.divider {
  display: flex;
  align-items: center;
  margin: 16px 0;
  color: #555;
  font-size: 0.85rem;
}
.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: #333;
}
.divider span {
  padding: 0 12px;
}

/* === Game Header === */
.game-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  border-bottom: 1px solid #333;
  margin-bottom: 16px;
}
.session-label {
  flex: 1;
  font-size: 0.9rem;
  color: #aaa;
}
.session-label strong {
  color: #fff;
}

/* === Share Panel === */
.share-panel {
  background: #16213e;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  animation: slideDown 0.25s ease-out;
}
.share-panel p {
  font-size: 0.9rem;
  margin-bottom: 8px;
}
.share-panel input[readonly] {
  background: #0f3460;
  border: 1px solid #333;
  border-radius: 4px;
  padding: 6px;
  color: #e0e0e0;
  width: 100%;
  font-size: 0.85rem;
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* === Token Display === */
.token-display {
  text-align: center;
  margin: 16px 0;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.prev-tokens {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
  min-height: 40px;
}
.current-token {
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 400px;
}

/* === Token Sprites (in-game display) === */
.token-sprite {
  background-image: url('/sprites/sprite.webp');
  background-repeat: no-repeat;
  display: inline-block;
  flex-shrink: 0;
  transform-style: preserve-3d;
}

/* large: 96px — 128 * 0.75 */
.token-sprite-large {
  width: 96px;
  height: 96px;
  background-size: calc(640px * 0.75) auto;
}

/* small: 40px — 128 * 0.3125 (matches bag modifier) */
.token-sprite-small {
  width: 40px;
  height: 40px;
  background-size: calc(640px * 0.3125) auto;
}

/* large positions (scale 0.75) */
.token-sprite-large.token-sprite-Auto-Fail  { background-position: calc(-0px   * 0.75) calc(-0px   * 0.75); }
.token-sprite-large.token-sprite-Bless       { background-position: calc(-128px * 0.75) calc(-0px   * 0.75); }
.token-sprite-large.token-sprite-Cultist     { background-position: calc(-0px   * 0.75) calc(-128px * 0.75); }
.token-sprite-large.token-sprite-Curse       { background-position: calc(-128px * 0.75) calc(-128px * 0.75); }
.token-sprite-large.token-sprite-Elder-Sign  { background-position: calc(-256px * 0.75) calc(-0px   * 0.75); }
.token-sprite-large.token-sprite-Frost       { background-position: calc(-256px * 0.75) calc(-128px * 0.75); }
.token-sprite-large.token-sprite-Elder-Thing { background-position: calc(-0px   * 0.75) calc(-256px * 0.75); }
.token-sprite-large.token-sprite-Skull       { background-position: calc(-128px * 0.75) calc(-256px * 0.75); }
.token-sprite-large.token-sprite-Tablet      { background-position: calc(-256px * 0.75) calc(-256px * 0.75); }
.token-sprite-large.token-sprite-p1          { background-position: calc(-384px * 0.75) calc(-0px   * 0.75); }
.token-sprite-large.token-sprite-m1          { background-position: calc(-384px * 0.75) calc(-128px * 0.75); }
.token-sprite-large.token-sprite-m2          { background-position: calc(-384px * 0.75) calc(-256px * 0.75); }
.token-sprite-large.token-sprite-m3          { background-position: calc(-0px   * 0.75) calc(-384px * 0.75); }
.token-sprite-large.token-sprite-m4          { background-position: calc(-128px * 0.75) calc(-384px * 0.75); }
.token-sprite-large.token-sprite-m5          { background-position: calc(-256px * 0.75) calc(-384px * 0.75); }
.token-sprite-large.token-sprite-m6          { background-position: calc(-384px * 0.75) calc(-384px * 0.75); }
.token-sprite-large.token-sprite-m7          { background-position: calc(-512px * 0.75) calc(-0px   * 0.75); }
.token-sprite-large.token-sprite-m8          { background-position: calc(-512px * 0.75) calc(-128px * 0.75); }
.token-sprite-large.token-sprite-0           { background-position: calc(-512px * 0.75) calc(-256px * 0.75); }

/* small positions (scale 0.3125) */
.token-sprite-small.token-sprite-Auto-Fail  { background-position: calc(-0px   * 0.3125) calc(-0px   * 0.3125); }
.token-sprite-small.token-sprite-Bless       { background-position: calc(-128px * 0.3125) calc(-0px   * 0.3125); }
.token-sprite-small.token-sprite-Cultist     { background-position: calc(-0px   * 0.3125) calc(-128px * 0.3125); }
.token-sprite-small.token-sprite-Curse       { background-position: calc(-128px * 0.3125) calc(-128px * 0.3125); }
.token-sprite-small.token-sprite-Elder-Sign  { background-position: calc(-256px * 0.3125) calc(-0px   * 0.3125); }
.token-sprite-small.token-sprite-Frost       { background-position: calc(-256px * 0.3125) calc(-128px * 0.3125); }
.token-sprite-small.token-sprite-Elder-Thing { background-position: calc(-0px   * 0.3125) calc(-256px * 0.3125); }
.token-sprite-small.token-sprite-Skull       { background-position: calc(-128px * 0.3125) calc(-256px * 0.3125); }
.token-sprite-small.token-sprite-Tablet      { background-position: calc(-256px * 0.3125) calc(-256px * 0.3125); }
.token-sprite-small.token-sprite-p1          { background-position: calc(-384px * 0.3125) calc(-0px   * 0.3125); }
.token-sprite-small.token-sprite-m1          { background-position: calc(-384px * 0.3125) calc(-128px * 0.3125); }
.token-sprite-small.token-sprite-m2          { background-position: calc(-384px * 0.3125) calc(-256px * 0.3125); }
.token-sprite-small.token-sprite-m3          { background-position: calc(-0px   * 0.3125) calc(-384px * 0.3125); }
.token-sprite-small.token-sprite-m4          { background-position: calc(-128px * 0.3125) calc(-384px * 0.3125); }
.token-sprite-small.token-sprite-m5          { background-position: calc(-256px * 0.3125) calc(-384px * 0.3125); }
.token-sprite-small.token-sprite-m6          { background-position: calc(-384px * 0.3125) calc(-384px * 0.3125); }
.token-sprite-small.token-sprite-m7          { background-position: calc(-512px * 0.3125) calc(-0px   * 0.3125); }
.token-sprite-small.token-sprite-m8          { background-position: calc(-512px * 0.3125) calc(-128px * 0.3125); }
.token-sprite-small.token-sprite-0           { background-position: calc(-512px * 0.3125) calc(-256px * 0.3125); }

/* fade-in animation for newly drawn token */
@keyframes tokenFlipReveal {
  0%   { opacity: 0; transform: rotateY(90deg) scale(0.8); }
  60%  { opacity: 1; transform: rotateY(-8deg) scale(1.05); }
  80%  { transform: rotateY(4deg) scale(1.0); }
  100% { transform: rotateY(0deg) scale(1); }
}
.token-fade-in {
  animation: tokenFlipReveal 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
}

@keyframes fadeIn {
  from { opacity: 0; transform: scale(0.9); }
  to   { opacity: 1; transform: scale(1); }
}

/* === Token Effect Text === */
.token-effect {
  max-width: 280px;
  margin-top: 12px;
  text-align: center;
  font-size: 0.9rem;
  line-height: 1.45;
  color: #8899bb;
  font-style: italic;
}
.token-effect.token-effect-animate {
  animation: effectFadeIn 0.4s ease-out 0.55s both;
}
@keyframes effectFadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* === Auto-Fail Banner === */
.auto-fail-banner {
  margin-top: 12px;
  padding: 12px 20px;
  background: #e94560;
  color: #fff;
  font-size: 1.3rem;
  font-weight: 700;
  border-radius: 8px;
  text-align: center;
  animation: fadeIn 0.2s ease-out;
}

/* === Pending Area (Bless/Curse) === */
.pending-area {
  border: 2px solid #ffc107;
  border-radius: 8px;
  padding: 12px;
  margin: 12px 0;
  text-align: center;
  background: rgba(255, 193, 7, 0.08);
}
.pending-token {
  font-size: 1.5rem;
  font-weight: 600;
  color: #ffc107;
}

/* === Consumed Area === */
.consumed-area {
  margin: 12px 0;
}
.consumed-area details {
  background: #16213e;
  border-radius: 8px;
  padding: 10px 14px;
}
.consumed-area summary {
  cursor: pointer;
  font-size: 0.9rem;
  color: #aaa;
}
.consumed-tokens {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.consumed-tokens .token {
  display: inline-block;
  font-size: 0.9rem;
  padding: 4px 10px;
  background: #333;
  color: #666;
  border-radius: 6px;
  text-decoration: line-through;
}

/* === Action Buttons === */
.actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 16px 0;
}

/* === Status Bar === */
.status-bar {
  padding: 12px 0;
  border-top: 1px solid #333;
  margin-top: auto;
}
#remaining-count {
  font-size: 1.4rem;
  font-weight: 700;
  color: #fff;
}
#remaining-details {
  margin-top: 6px;
}
#remaining-details summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: #888;
}
#remaining-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
.remaining-token-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 0.8rem;
  color: #aaa;
}

/* === Lock Info === */
.lock-info {
  background: #ffa500;
  color: #1a1a2e;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  margin: 12px 0;
}

/* === Toast === */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #e94560;
  color: #fff;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  z-index: 1000;
  animation: toastIn 0.3s ease-out, toastOut 0.3s ease-in 2.7s forwards;
  white-space: nowrap;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@keyframes toastOut {
  from { opacity: 1; }
  to   { opacity: 0; }
}

/* === Reconnect Banner === */
.reconnect-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #ffc107;
  color: #1a1a2e;
  text-align: center;
  padding: 10px;
  font-weight: 600;
  font-size: 0.9rem;
  z-index: 999;
}

/* panels inside bottom sheet must fill and flex */
#modify-panel-manual,
#modify-panel-scenario {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* === Modal Overlay — bottom sheet === */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 900;
  animation: fadeIn 0.15s ease-out;
}
.modal-content {
  background: #16213e;
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s cubic-bezier(0.32, 0.72, 0, 1);
  overflow: hidden;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* drag handle */
.modal-content::before {
  content: '';
  display: block;
  width: 40px;
  height: 4px;
  background: #444;
  border-radius: 2px;
  margin: 12px auto 0;
  flex-shrink: 0;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px 10px;
  flex-shrink: 0;
  border-bottom: 1px solid #253555;
}
.modal-header h2 {
  font-size: 1.15rem;
  margin: 0;
}
.modal-header-actions {
  display: flex;
  gap: 8px;
}

/* scrollable token list */
.modify-grid {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  -webkit-overflow-scrolling: touch;
}

/* sticky footer with action buttons */
.modal-footer {
  display: flex;
  gap: 10px;
  padding: 12px 20px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid #253555;
  background: #16213e;
  flex-shrink: 0;
}
.modal-footer .btn {
  flex: 1;
}

/* scenario form scroll area */
.modal-scrollable {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px;
  -webkit-overflow-scrolling: touch;
}

/* === Modify Bag Rows === */
.modify-row {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 20px;
  gap: 10px;
  border-bottom: 1px solid #1e2d4a;
  transition: background 0.1s;
}
.modify-row:last-child {
  border-bottom: none;
}
.modify-row:active {
  background: rgba(255,255,255,0.03);
}

/* token sprite scaled down to 40px */
.modify-token-sprite {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  background-image: url('/sprites/sprite.webp');
  background-repeat: no-repeat;
  background-size: calc(640px * 0.3125) auto; /* 128*5cols * scale = 200px wide sheet */
}

/* sprite positions scaled by factor 40/128 = 0.3125 */
.modify-sprite-Auto-Fail       { background-position: calc(-0px   * 0.3125) calc(-0px   * 0.3125); }
.modify-sprite-Bless            { background-position: calc(-128px * 0.3125) calc(-0px   * 0.3125); }
.modify-sprite-Cultist          { background-position: calc(-0px   * 0.3125) calc(-128px * 0.3125); }
.modify-sprite-Curse            { background-position: calc(-128px * 0.3125) calc(-128px * 0.3125); }
.modify-sprite-Elder-Sign       { background-position: calc(-256px * 0.3125) calc(-0px   * 0.3125); }
.modify-sprite-Frost            { background-position: calc(-256px * 0.3125) calc(-128px * 0.3125); }
.modify-sprite-Elder-Thing      { background-position: calc(-0px   * 0.3125) calc(-256px * 0.3125); }
.modify-sprite-Skull            { background-position: calc(-128px * 0.3125) calc(-256px * 0.3125); }
.modify-sprite-Tablet           { background-position: calc(-256px * 0.3125) calc(-256px * 0.3125); }
.modify-sprite-p1               { background-position: calc(-384px * 0.3125) calc(-0px   * 0.3125); }
.modify-sprite-m1               { background-position: calc(-384px * 0.3125) calc(-128px * 0.3125); }
.modify-sprite-m2               { background-position: calc(-384px * 0.3125) calc(-256px * 0.3125); }
.modify-sprite-m3               { background-position: calc(-0px   * 0.3125) calc(-384px * 0.3125); }
.modify-sprite-m4               { background-position: calc(-128px * 0.3125) calc(-384px * 0.3125); }
.modify-sprite-m5               { background-position: calc(-256px * 0.3125) calc(-384px * 0.3125); }
.modify-sprite-m6               { background-position: calc(-384px * 0.3125) calc(-384px * 0.3125); }
.modify-sprite-m7               { background-position: calc(-512px * 0.3125) calc(-0px   * 0.3125); }
.modify-sprite-m8               { background-position: calc(-512px * 0.3125) calc(-128px * 0.3125); }
.modify-sprite-0                { background-position: calc(-512px * 0.3125) calc(-256px * 0.3125); }

.modify-count {
  font-size: 1.05rem;
  font-weight: 600;
  color: #c8cfe0;
  min-width: 40px;
  text-align: center;
}

.btn-modify-minus,
.btn-modify-plus {
  flex-shrink: 0;
  width: auto;
  min-width: 44px;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 22px;
  font-size: 1.4rem;
  font-weight: 300;
  line-height: 1;
}

.btn-modify-minus {
  background: #1e2d4a;
  color: #e0e0e0;
}
.btn-modify-minus:hover:not(:disabled) {
  background: #253555;
}
.btn-modify-minus:disabled {
  background: #1a2235 !important;
  color: #3a4a62 !important;
  opacity: 1 !important;
}

.btn-modify-plus {
  background: #e94560;
  color: #fff;
}
.btn-modify-plus:hover:not(:disabled) {
  background: #d63850;
}

/* === Scrollbar === */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: #1a1a2e;
}
::-webkit-scrollbar-thumb {
  background: #555;
  border-radius: 3px;
}

/* === Responsive === */
@media (min-width: 481px) {
  body {
    padding: 24px;
  }
  .modal-content {
    border-radius: 20px 20px 0 0;
  }
}

/* === Select inputs (scenario picker) === */
.form-group select {
  width: 100%;
  padding: 12px 14px;
  background: #0f3460;
  border: 1px solid #1a4a7a;
  border-radius: 8px;
  color: #e0e0e0;
  font-size: 1rem;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23888' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 36px;
}
.form-group select:focus {
  border-color: #e94560;
}
.form-group select:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* === Connect screen: two equal-width action buttons === */
.connect-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.connect-actions .btn {
  flex: 1;
}

/* === Join Dialog overlay === */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 950;
  animation: fadeIn 0.15s ease-out;
}
.dialog-card {
  background: #16213e;
  border-radius: 12px;
  padding: 24px;
  width: calc(100% - 48px);
  max-width: 360px;
}
.dialog-card h2 {
  font-size: 1.15rem;
  margin-bottom: 16px;
}
.dialog-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.dialog-actions .btn {
  flex: 1;
}

/* === Scenario picker screen === */
#view-scenario {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}
#view-scenario.active {
  display: flex;
}
#view-scenario .scenario-form {
  margin-bottom: 4px;
}

/* === URL-join screen === */
#view-url-join {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
}
#view-url-join.active {
  display: flex;
}
.url-join-banner {
  background: #0f3460;
  border: 1px solid #1a4a7a;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
  color: #aaa;
  margin-bottom: 16px;
  text-align: center;
}
.url-join-banner strong {
  color: #fff;
  font-family: monospace;
  font-size: 1rem;
}