* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #f3efe7; color: #2c3238;
  font-family: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
#c { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
button { font: inherit; color: inherit; cursor: pointer; }

.top { position: fixed; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 10px; pointer-events: none; }
.top > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 42px; height: 42px; border-radius: 12px; background: #f5b82e; color: #fff; display: grid; place-items: center;
  font-size: 26px; box-shadow: 0 3px 0 #c98f12; animation: spin 8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.brand b { display: block; font-size: 19px; letter-spacing: .5px; }
.brand small { color: #7b848c; font-size: 13px; }
.modes { display: flex; background: #fff; border-radius: 999px; padding: 4px; box-shadow: 0 4px 14px rgba(40,40,30,.08); margin-left: auto; }
.modes button { border: 0; background: none; padding: 9px 18px; border-radius: 999px; font-size: 16px; font-weight: 600; color: #6a737b; transition: .2s; }
.modes button.on { background: #2c3238; color: #fff; }
.pill { border: 0; background: #fff; padding: 9px 16px; border-radius: 999px; font-size: 15px; font-weight: 600;
  box-shadow: 0 4px 14px rgba(40,40,30,.08); transition: transform .15s; }
.pill:active { transform: scale(.94); }

.bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom) + 14px);
  background: rgba(255,255,255,.92); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-radius: 22px; box-shadow: 0 8px 28px rgba(40,40,30,.12); padding: 12px 16px; max-width: calc(100vw - 24px); }
#viewBar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center; }
.gears { display: flex; align-items: center; gap: 6px; }
.lbl { font-size: 13px; color: #7b848c; margin-right: 2px; }
.gears button { width: 46px; height: 46px; border-radius: 14px; border: 2px solid #e4e0d8; background: #faf8f4; font-size: 20px; font-weight: 800; transition: .15s; }
.gears button.on { background: #d7263d; border-color: #d7263d; color: #fff; box-shadow: 0 3px 0 #9e1a2b; transform: translateY(-2px); }
#spinBtn.on { background: #2c3238; color: #fff; }
.speed { font-size: 13px; color: #7b848c; display: flex; align-items: center; gap: 6px; }
.speed input { width: 110px; accent-color: #f5b82e; }
#readout { min-width: 240px; max-width: 420px; font-size: 14px; line-height: 1.5; }
#readout b { display: block; font-size: 16px; }
#readout span { color: #5b646c; }
.count { margin-top: 4px; font-variant-numeric: tabular-nums; color: #2c3238; }
.count em { font-style: normal; font-weight: 800; font-size: 18px; color: #d7263d; }

#buildBar { width: min(1100px, calc(100vw - 24px)); padding: 10px 12px; }
.status { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 15px; margin-bottom: 8px; }
#prog { font-weight: 800; }
#timer { font-variant-numeric: tabular-nums; background: #2c3238; color: #fff; padding: 3px 10px; border-radius: 8px; }
#best { color: #7b848c; font-size: 13px; }
.sw { display: flex; align-items: center; gap: 4px; font-size: 14px; color: #5b646c; margin-left: auto; }
.sw input { width: 18px; height: 18px; accent-color: #2f9e44; }
.status .pill { box-shadow: none; background: #f3efe7; padding: 7px 12px; font-size: 14px; }
#tray { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; max-height: 220px; overflow: hidden; }
.tray-item { position: relative; width: 74px; border: 2px solid #ece8e0; background: #fff; border-radius: 16px; padding: 4px 2px 6px;
  touch-action: none; transition: transform .15s, opacity .2s; }
.tray-item img { width: 54px; height: 54px; display: block; margin: 0 auto; pointer-events: none; }
.tray-item span { display: block; font-size: 13px; font-weight: 600; text-align: center; pointer-events: none; }
.tray-item em { position: absolute; top: 4px; right: 6px; font-style: normal; font-size: 12px; font-weight: 800; color: #fff;
  background: #2f7fe0; border-radius: 999px; padding: 0 6px; pointer-events: none; }
.tray-item.dragging { opacity: .35; }
.tray-item.pulse { animation: pulse .6s ease-in-out 4; border-color: #f5b82e; }
@keyframes pulse { 50% { transform: scale(1.12); } }

#card { position: fixed; right: 16px; top: 84px; width: min(360px, calc(100vw - 32px)); max-height: calc(100vh - 230px); overflow: auto;
  background: #fff; border-radius: 22px; padding: 18px 20px 16px; box-shadow: 0 10px 34px rgba(40,40,30,.16);
  animation: pop .35s cubic-bezier(.2,1.4,.4,1); -webkit-user-select: text; user-select: text; }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.94); } }
#card .x, .sheet .x { position: absolute; top: 10px; right: 12px; border: 0; background: #f3efe7; width: 34px; height: 34px; border-radius: 50%; font-size: 16px; }
#card .tag { display: inline-block; background: var(--c); color: #fff; font-size: 12px; font-weight: 700; padding: 2px 10px; border-radius: 999px; }
#card h2 { margin: 8px 0 6px; font-size: 22px; }
#card h3 { margin: 12px 0 4px; font-size: 14px; color: #7b848c; }
#card p { margin: 0; font-size: 15px; line-height: 1.65; }
#card .one { font-size: 16px; font-weight: 600; }

#tip { position: fixed; left: 50%; transform: translateX(-50%); top: calc(env(safe-area-inset-top) + 74px); font-size: 13px; color: #7b848c;
  background: rgba(255,255,255,.7); padding: 4px 12px; border-radius: 999px; pointer-events: none; white-space: nowrap; }
#toast { position: fixed; left: 50%; top: 40%; transform: translate(-50%, -50%) scale(.9); background: #2c3238; color: #fff; padding: 12px 20px;
  border-radius: 16px; font-size: 17px; font-weight: 600; opacity: 0; pointer-events: none; transition: .25s; max-width: 80vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
#toast.warn { background: #e8792a; }
#toast.good { background: #2f9e44; }

#lesson { position: fixed; inset: 0; background: rgba(30,30,25,.35); display: grid; place-items: center; z-index: 10; }
#lesson[hidden] { display: none; }
.sheet { position: relative; width: min(640px, 92vw); max-height: 86vh; overflow: auto; background: #fff; border-radius: 24px; padding: 24px 28px;
  -webkit-user-select: text; user-select: text; animation: pop .35s cubic-bezier(.2,1.4,.4,1); }
.sheet h2 { margin: 0 0 10px; }
.sheet h3 { margin: 18px 0 6px; color: #d7263d; font-size: 17px; }
.sheet p { margin: 6px 0; line-height: 1.75; font-size: 16px; }
.formula { background: #fff6dd; border-radius: 12px; padding: 10px 14px; font-weight: 700; text-align: center; }
.sheet details { background: #f3efe7; border-radius: 12px; padding: 10px 14px; margin: 8px 0; font-size: 15px; }
.sheet summary { font-weight: 700; cursor: pointer; }

#loading { position: fixed; inset: 0; display: grid; place-items: center; background: #f3efe7; font-size: 18px; color: #7b848c; z-index: 20; transition: opacity .4s; }

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 15; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(var(--r)); } }

@media (max-width: 760px), (orientation: portrait) {
  .modes { margin-left: 0; }
  #card { top: auto; bottom: 200px; right: 50%; transform: translateX(50%); max-height: 42vh; }
  @keyframes pop { from { opacity: 0; } }
  #readout { min-width: 0; width: 100%; text-align: center; }
  #tip { top: auto; bottom: calc(env(safe-area-inset-bottom) + 200px); }
}
