* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  overflow: hidden; background: #1a1a2e;
  font-family: -apple-system, 'SF Pro Text', 'Segoe UI', system-ui, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  height: 100%; width: 100%;
  user-select: none; -webkit-user-select: none;
}
canvas { display: block; touch-action: none; }

/* ============ Screens ============ */
.screen {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 10, 25, 0.92);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
}

/* Mode Select */
.mode-select-inner { text-align: center; padding: 20px; }
.mode-title {
  color: #fff; font-size: 32px; font-weight: 800;
  letter-spacing: -0.02em;
}
.mode-subtitle {
  color: rgba(255,255,255,0.4); font-size: 15px; margin-top: 6px;
}
.mode-cards {
  display: flex; gap: 16px; margin-top: 32px; justify-content: center;
  flex-wrap: wrap;
}
.mode-card {
  width: 180px; padding: 28px 16px 24px;
  background: rgba(44, 44, 46, 0.8);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 16px; cursor: pointer;
  transition: all 0.2s; text-align: center;
  -webkit-tap-highlight-color: transparent;
}
.mode-card:active { transform: scale(0.96); }
@media (hover: hover) {
  .mode-card:hover {
    background: rgba(58, 58, 60, 0.9);
    border-color: rgba(10, 132, 255, 0.5);
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  }
}
.mode-card-icon { font-size: 40px; margin-bottom: 12px; }
.mode-card-name { color: #fff; font-size: 17px; font-weight: 700; }
.mode-card-desc {
  color: rgba(255,255,255,0.4); font-size: 12px; margin-top: 8px; line-height: 1.5;
}

/* Level Select */
.level-select-inner {
  width: 100%; max-width: 460px; max-height: 90vh;
  padding: 20px; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}
.level-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px;
}
.level-back-btn {
  background: none; border: none; color: #0a84ff; font-size: 16px;
  font-weight: 600; cursor: pointer; padding: 4px 0;
  -webkit-tap-highlight-color: transparent;
}
.level-back-btn:active { opacity: 0.5; }
.level-title { color: #fff; font-size: 20px; font-weight: 700; }

.level-section { margin-bottom: 16px; }
.level-section-header {
  color: rgba(255,255,255,0.5); font-size: 13px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.04em;
  padding: 0 4px 8px;
}
.level-row {
  display: flex; gap: 8px; flex-wrap: wrap;
}
.level-btn {
  width: 38px; height: 38px; border-radius: 10px; border: none;
  font-size: 13px; font-weight: 700; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s; -webkit-tap-highlight-color: transparent;
  position: relative;
}
.level-btn.locked {
  background: rgba(255,255,255,0.06); color: rgba(255,255,255,0.2);
  cursor: default;
}
.level-btn.unlocked {
  background: rgba(10, 132, 255, 0.2); color: #0a84ff;
  border: 1px solid rgba(10, 132, 255, 0.3);
}
.level-btn.unlocked:active { transform: scale(0.9); }
.level-btn.cleared {
  background: rgba(48, 209, 88, 0.15); color: #30d158;
  border: 1px solid rgba(48, 209, 88, 0.3);
}
.level-btn .check-mark {
  position: absolute; top: -2px; right: -2px;
  font-size: 10px; color: #30d158;
}

/* Challenge Panel */
#challenge-panel {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 16px 12px;
  pointer-events: none;
}
#challenge-panel > * { pointer-events: auto; }
.challenge-info {
  color: rgba(255,255,255,0.6); font-size: 14px; font-weight: 600;
  background: rgba(30,30,34,0.72);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 20px; padding: 6px 18px;
  margin-bottom: 8px;
}

/* iOS row link style (with arrow) */
.ios-row-link { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.ios-row-link:active { background: rgba(255,255,255,0.04); }
.ios-row-arrow {
  color: rgba(255,255,255,0.25); font-size: 22px; font-weight: 300;
  line-height: 1; margin-left: 4px;
}

/* ============ Formula Screen ============ */
#formula-screen { z-index: 60; }
.formula-inner {
  width: 100%; max-width: 520px; height: 100%;
  display: flex; flex-direction: column;
  padding: 20px 16px 0;
}
.formula-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; flex-shrink: 0;
}
.formula-title { color: #fff; font-size: 20px; font-weight: 700; }

.formula-tabs {
  display: flex; gap: 0; flex-shrink: 0;
  background: #2c2c2e; border-radius: 10px;
  padding: 3px; margin-bottom: 16px;
}
.formula-tab {
  flex: 1; padding: 8px 0; border: none; border-radius: 8px;
  background: transparent; color: rgba(255,255,255,0.5);
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: all 0.2s; -webkit-tap-highlight-color: transparent;
}
.formula-tab.active {
  background: #636366; color: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

.formula-content {
  flex: 1; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding-bottom: 24px;
}

.formula-step {
  background: #2c2c2e; border-radius: 12px;
  padding: 16px; margin-bottom: 12px;
}
.formula-step-header {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 10px;
}
.formula-step-num {
  width: 28px; height: 28px; border-radius: 50%;
  background: #0a84ff; color: #fff;
  font-size: 14px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.formula-step-name {
  color: #fff; font-size: 16px; font-weight: 600;
}
.formula-step-desc {
  color: rgba(255,255,255,0.5); font-size: 13px;
  line-height: 1.5; margin-bottom: 12px;
}
.formula-list { display: flex; flex-direction: column; gap: 8px; }
.formula-item {
  background: rgba(0,0,0,0.25); border-radius: 8px;
  padding: 10px 14px;
}
.formula-item-name {
  color: rgba(255,255,255,0.4); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.formula-item-moves {
  color: #fff; font-size: 17px; font-weight: 700;
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  letter-spacing: 0.5px; word-spacing: 4px;
}
.formula-item-note {
  color: rgba(255,255,255,0.35); font-size: 12px;
  margin-top: 4px; line-height: 1.4;
}

/* Move notation colors */
.m-R { color: #ff453a; } .m-L { color: #ff9f0a; }
.m-U { color: #ffffff; } .m-D { color: #ffd60a; }
.m-F { color: #30d158; } .m-B { color: #64d2ff; }

/* Formula demo button */
.formula-demo-btn {
  display: inline-flex; align-items: center; gap: 4px;
  margin-top: 8px; padding: 5px 12px;
  background: rgba(10, 132, 255, 0.2); color: #0a84ff;
  border: 1px solid rgba(10, 132, 255, 0.3);
  border-radius: 8px; font-size: 12px; font-weight: 600;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}
.formula-demo-btn:active { transform: scale(0.95); }
@media (hover: hover) {
  .formula-demo-btn:hover { background: rgba(10, 132, 255, 0.35); }
}

/* Demo panel */
#demo-panel {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 16px 12px;
  pointer-events: none;
}
#demo-panel > * { pointer-events: auto; }
.demo-info {
  color: #fff; font-size: 15px; font-weight: 600;
  background: rgba(30,30,34,0.72);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 20px; padding: 6px 20px;
  margin-bottom: 8px; text-align: center;
  max-width: 90vw;
}
.demo-steps {
  display: flex; gap: 6px; align-items: center;
  margin-bottom: 8px; flex-wrap: wrap; justify-content: center;
  max-width: 90vw;
}
.demo-step {
  width: 36px; height: 36px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  transition: all 0.2s;
}
.demo-step.pending {
  background: rgba(255,255,255,0.08); color: rgba(255,255,255,0.3);
}
.demo-step.current {
  background: rgba(10, 132, 255, 0.8); color: #fff;
  transform: scale(1.1);
  box-shadow: 0 2px 8px rgba(10,132,255,0.4);
}
.demo-step.done {
  background: rgba(48, 209, 88, 0.25); color: #30d158;
}

/* Win Dialog */
.win-dialog { text-align: center; padding: 32px 24px 24px; }
.win-icon { font-size: 48px; margin-bottom: 8px; }
.win-title { color: #fff; font-size: 22px; font-weight: 700; margin-bottom: 16px; }
.win-stats {
  display: flex; gap: 32px; justify-content: center; margin-bottom: 24px;
}
.win-stat { display: flex; flex-direction: column; align-items: center; }
.win-stat-val {
  color: #fff; font-size: 24px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.win-stat-label {
  color: rgba(255,255,255,0.4); font-size: 12px; margin-top: 4px;
  text-transform: uppercase; letter-spacing: 0.04em;
}
.win-btns { display: flex; gap: 12px; justify-content: center; }

/* ============ Top Panel ============ */
#top-panel {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 16px 12px;
  pointer-events: none;
}
#top-panel > * { pointer-events: auto; }

/* Status bar (timer + steps) */
.status-bar {
  display: flex; align-items: center;
  background: rgba(30, 30, 34, 0.72);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 16px;
  padding: 10px 24px;
  gap: 0; margin-top: 10px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.3);
  opacity: 0; transform: translateY(-6px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}
.status-bar.visible {
  opacity: 1; transform: translateY(0);
  pointer-events: auto;
}
.status-card {
  display: flex; flex-direction: column; align-items: center;
  padding: 0 16px; min-width: 72px;
}
.status-value {
  color: #fff; font-size: 22px; font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.status-label {
  color: rgba(255,255,255,0.4); font-size: 11px; font-weight: 500;
  text-transform: uppercase; letter-spacing: 0.05em;
  margin-top: 2px;
}
.status-divider {
  width: 1px; height: 28px;
  background: rgba(255,255,255,0.1);
}

/* Action buttons row */
.btn-row {
  display: flex; gap: 8px; align-items: center; margin-top: 12px;
}

.action-btn {
  display: flex; align-items: center; gap: 5px;
  padding: 8px 16px; border: none; border-radius: 12px;
  cursor: pointer; font-size: 13px; font-weight: 600;
  color: #fff; transition: all 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.06);
  box-shadow: 0 1px 6px rgba(0,0,0,0.2);
}
.btn-icon {
  width: 16px; height: 16px; flex-shrink: 0;
}

.btn-red { background: rgba(255, 69, 58, 0.75); }
.btn-blue { background: rgba(10, 132, 255, 0.75); }
.btn-green { background: rgba(48, 209, 88, 0.7); }
.btn-yellow { background: rgba(255, 214, 10, 0.7); color: rgba(0,0,0,0.85); }
.btn-yellow .btn-icon { stroke: rgba(0,0,0,0.85); }
.btn-gray { background: rgba(99, 99, 102, 0.5); padding: 8px 10px; }
.btn-purple { background: rgba(175, 82, 222, 0.75); }

@media (hover: hover) {
  .btn-red:hover { background: rgba(255, 69, 58, 0.95); transform: translateY(-1px); box-shadow: 0 3px 12px rgba(255,69,58,0.3); }
  .btn-blue:hover { background: rgba(10, 132, 255, 0.95); transform: translateY(-1px); box-shadow: 0 3px 12px rgba(10,132,255,0.3); }
  .btn-green:hover { background: rgba(48, 209, 88, 0.9); transform: translateY(-1px); box-shadow: 0 3px 12px rgba(48,209,88,0.3); }
  .btn-yellow:hover { background: rgba(255, 214, 10, 0.9); transform: translateY(-1px); box-shadow: 0 3px 12px rgba(255,214,10,0.3); }
  .btn-gray:hover { background: rgba(99, 99, 102, 0.7); transform: translateY(-1px); }
  .btn-purple:hover { background: rgba(175, 82, 222, 0.95); transform: translateY(-1px); box-shadow: 0 3px 12px rgba(175,82,222,0.3); }
}
.action-btn:active { transform: scale(0.94); box-shadow: none; }

/* ============ Settings Modal (iOS style) ============ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease;
}
.modal-overlay.show {
  opacity: 1; pointer-events: auto;
}

.modal-dialog {
  width: 340px; max-width: 92vw;
  max-height: 85vh;
  background: #1c1c1e;
  border-radius: 14px;
  overflow: hidden;
  transform: scale(0.95) translateY(10px);
  transition: transform 0.25s ease;
  display: flex; flex-direction: column;
}
.modal-overlay.show .modal-dialog {
  transform: scale(1) translateY(0);
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.modal-title {
  color: #fff; font-size: 17px; font-weight: 600;
}
.modal-close-btn {
  background: none; border: none; color: #0a84ff; font-size: 17px;
  font-weight: 600; cursor: pointer; padding: 0;
  -webkit-tap-highlight-color: transparent;
}
.modal-close-btn:active { opacity: 0.5; }

.modal-body {
  padding: 8px 16px 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

/* iOS-style grouped sections */
.ios-section {
  margin-top: 16px;
}
.ios-section:first-child { margin-top: 8px; }

.ios-section-header {
  color: rgba(255,255,255,0.4);
  font-size: 13px; font-weight: 500;
  text-transform: uppercase;
  padding: 0 4px 6px;
  letter-spacing: 0.02em;
}

.ios-section-footer {
  color: rgba(255,255,255,0.3);
  font-size: 12px; padding: 6px 4px 0;
  line-height: 1.4;
}

.ios-card {
  background: #2c2c2e;
  border-radius: 10px;
  overflow: hidden;
}

.ios-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  min-height: 44px;
}
.ios-row + .ios-row {
  border-top: 1px solid rgba(255,255,255,0.06);
}

.ios-row-label {
  color: #fff; font-size: 15px;
}

.ios-row-control {
  display: flex; align-items: center; gap: 6px;
}

/* iOS-style stepper */
.stepper {
  display: flex; align-items: center; gap: 0;
  background: #39393d; border-radius: 9px;
  overflow: hidden;
}
.stepper-btn {
  width: 36px; height: 32px; border: none;
  background: transparent; color: #0a84ff;
  font-size: 20px; font-weight: 500; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s;
}
.stepper-btn:active { background: rgba(255,255,255,0.08); }
.stepper-value {
  width: 32px; text-align: center;
  color: #fff; font-size: 16px; font-weight: 700;
  font-variant-numeric: tabular-nums;
  border-left: 1px solid rgba(255,255,255,0.1);
  border-right: 1px solid rgba(255,255,255,0.1);
  line-height: 32px;
}

/* iOS-style toggle switch */
.ios-switch {
  position: relative; display: inline-block;
  width: 51px; height: 31px;
  -webkit-tap-highlight-color: transparent;
}
.ios-switch input {
  opacity: 0; width: 0; height: 0;
}
.ios-switch-slider {
  position: absolute; inset: 0; cursor: pointer;
  background: #39393d; border-radius: 31px;
  transition: background 0.25s;
}
.ios-switch-slider::before {
  content: ''; position: absolute;
  width: 27px; height: 27px; left: 2px; bottom: 2px;
  background: #fff; border-radius: 50%;
  transition: transform 0.25s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.ios-switch input:checked + .ios-switch-slider {
  background: #34c759;
}
.ios-switch input:checked + .ios-switch-slider::before {
  transform: translateX(20px);
}

/* Sliders in settings */
.slider-control {
  display: flex; align-items: center; gap: 8px;
}
.slider-control input[type="range"] {
  width: 120px; height: 4px;
  accent-color: #0a84ff; cursor: pointer;
  -webkit-appearance: none; appearance: none;
  background: #48484a; border-radius: 2px; outline: none;
}
.slider-control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff; cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.slider-val {
  color: rgba(255,255,255,0.5); font-size: 13px;
  min-width: 40px; text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Hint toast */
#hint-toast {
  position: absolute; z-index: 10;
  background: rgba(0,0,0,0.75); backdrop-filter: blur(12px);
  border: 1px solid rgba(241, 196, 15, 0.4); border-radius: 12px;
  padding: 14px 22px;
  opacity: 0; pointer-events: none; transition: opacity 0.3s;
  text-align: center;
  top: 50%; right: 40px; transform: translateY(-50%);
}
#hint-toast.show { opacity: 1; pointer-events: auto; }
#hint-toast .hint-label { color: rgba(255,255,255,0.5); font-size: 12px; margin-bottom: 4px; }
#hint-toast .hint-move {
  color: #f1c40f; font-size: 36px; font-weight: 800; font-family: monospace;
}
#hint-toast .hint-desc { color: rgba(255,255,255,0.6); font-size: 12px; margin-top: 4px; }

/* Bottom hint */
#bottom-hint {
  position: absolute; bottom: 36px; left: 50%; transform: translateX(-50%);
  color: rgba(255,255,255,0.4); font-size: 12px; z-index: 10;
  white-space: nowrap;
}

#copyright {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  color: rgba(255,255,255,0.2); font-size: 10px; z-index: 10;
  white-space: nowrap; letter-spacing: 0.02em;
}

/* ============ Mobile (<=640px) ============ */
@media (max-width: 640px) {
  #top-panel { padding: 10px 8px 8px; }

  .status-bar { padding: 8px 16px; border-radius: 14px; }
  .status-card { padding: 0 12px; min-width: 60px; }
  .status-value { font-size: 18px; }
  .status-label { font-size: 10px; }
  .status-divider { height: 22px; }

  .btn-row { gap: 5px; margin-top: 10px; }
  .action-btn { padding: 7px 12px; font-size: 12px; border-radius: 10px; gap: 4px; }
  .btn-icon { width: 14px; height: 14px; }
  .btn-gray { padding: 7px 9px; }

  .modal-dialog { width: 92vw; }
  .mode-title { font-size: 26px; }
  .mode-cards { gap: 12px; }
  .mode-card { width: 156px; padding: 22px 12px 20px; }
  .mode-card-icon { font-size: 32px; }
  .mode-card-name { font-size: 15px; }
  .mode-card-desc { font-size: 11px; }
  .level-btn { width: 34px; height: 34px; font-size: 12px; }
  #challenge-panel { padding: 10px 8px 8px; }
  .challenge-info { font-size: 13px; padding: 5px 14px; }
  .formula-inner { padding: 16px 12px 0; }
  .formula-step { padding: 14px; }
  .formula-item-moves { font-size: 15px; }
  .formula-tab { font-size: 13px; padding: 7px 0; }

  #hint-toast {
    top: auto; right: auto; bottom: 60px;
    left: 50%; transform: translateX(-50%);
  }

  #bottom-hint { font-size: 11px; bottom: 34px; }
  #copyright { bottom: 14px; }
}

@media (max-width: 380px) {
  .action-btn { padding: 6px 9px; font-size: 11px; }
  .btn-icon { width: 12px; height: 12px; }
  .status-bar { padding: 6px 12px; }
  .status-value { font-size: 16px; }
  .slider-control input[type="range"] { width: 90px; }
}
