/* ===================== Dun2Traffic — tema cozy ===================== */
:root {
  --bg: #f7efe2;
  --card: #fffaf2;
  --card-2: #fbf2e4;
  --ink: #4a4058;
  --ink-soft: #6c6178;
  --muted: #8f859b;
  --line: #eadfcd;
  --primary: #f08a7e;
  --primary-d: #d9695d;
  --sage: #8cc29f;
  --sage-d: #5f9c76;
  --sky: #a8d3ea;
  --butter: #f6d27a;
  --danger: #e57373;
  --shadow: 0 10px 30px rgba(110, 80, 60, .14);
  --shadow-sm: 0 4px 12px rgba(110, 80, 60, .12);
  --radius: 22px;
  --font: 'Fredoka', 'Nunito', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none; -webkit-user-select: none;
}
input, select, textarea { user-select: text; -webkit-user-select: text; }
button { font-family: inherit; }
[hidden] { display: none !important; }

#bg-canvas { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; display: block; }

/* ---------- Screens ---------- */
.screen {
  position: fixed; inset: 0; z-index: 1;
  display: none; flex-direction: column; align-items: center;
  overflow-y: auto; overflow-x: hidden;
  padding: 16px;
}
.screen.active { display: flex; animation: fadeIn .45s ease; }
.game-screen { padding: 0; overflow: hidden; background: #cfe6b8; z-index: 2; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- Logo ---------- */
.logo {
  font-weight: 700; letter-spacing: .5px; color: var(--ink);
  text-shadow: 0 3px 0 #fff, 0 6px 18px rgba(240, 138, 126, .25);
  line-height: 1;
}
.logo span { color: var(--primary); display: inline-block; transform: rotate(-8deg); }
.logo.big { font-size: clamp(2.4rem, 8vw, 3.6rem); }
.logo.huge { font-size: clamp(2.6rem, 10vw, 4.6rem); }
.tagline { margin: 10px 0 18px; color: var(--ink-soft); text-align: center; font-size: 1.05rem; }

/* ---------- Loading ---------- */
.loading-box { margin: auto; text-align: center; }
.loader-car { position: relative; width: 90px; height: 50px; margin: 26px auto 10px; animation: bob 0.6s ease-in-out infinite alternate; }
.lc-body { position: absolute; left: 0; right: 0; bottom: 12px; height: 26px; background: var(--primary); border-radius: 14px 18px 8px 8px; }
.lc-body::before { content: ''; position: absolute; left: 18px; top: -16px; width: 44px; height: 20px; background: var(--primary); border-radius: 12px 12px 0 0; }
.lc-body::after { content: ''; position: absolute; left: 24px; top: -11px; width: 32px; height: 10px; background: #d8eef8; border-radius: 6px; }
.lc-wheel { position: absolute; bottom: 2px; width: 22px; height: 22px; border-radius: 50%; background: #3d3a4a; border: 5px solid #6b6578; animation: spin .5s linear infinite; }
.lc-wheel.a { left: 10px; } .lc-wheel.b { right: 10px; }
@keyframes bob { to { transform: translateY(-4px); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Cards / buttons ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); border: 2px solid #fff; }
.btn {
  border: 0; cursor: pointer; border-radius: 16px; padding: 12px 18px;
  font-size: 1rem; font-weight: 600; color: var(--ink);
  background: var(--card-2); box-shadow: 0 4px 0 var(--line);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s;
}
.btn:hover { filter: brightness(1.03); transform: translateY(-1px); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--primary); color: #fff; box-shadow: 0 4px 0 var(--primary-d); }
.btn.primary:active { box-shadow: 0 1px 0 var(--primary-d); }
.btn.soft { background: #eef6ea; color: var(--sage-d); box-shadow: 0 4px 0 #d3e6cc; }
.btn.danger { background: #fde3e0; color: #c4554b; box-shadow: 0 4px 0 #f3c6c0; }
.btn.block { display: block; width: 100%; margin-top: 10px; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--card); box-shadow: var(--shadow-sm); font-size: 1.15rem;
}
.icon-btn:active { transform: scale(.94); }

/* ---------- Auth ---------- */
.auth-wrap { margin: auto; width: 100%; max-width: 400px; display: flex; flex-direction: column; align-items: center; padding: 20px 0; }
.auth-card { width: 100%; padding: 22px; }
.tabs { display: flex; background: var(--card-2); border-radius: 16px; padding: 5px; gap: 5px; margin-bottom: 16px; }
.tab { flex: 1; border: 0; background: transparent; padding: 10px; border-radius: 12px; font-weight: 600; font-size: 1rem; color: var(--muted); cursor: pointer; }
.tab.active { background: #fff; color: var(--ink); box-shadow: var(--shadow-sm); }
.tabs.small .tab { font-size: .9rem; padding: 8px 6px; }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-weight: 500; font-size: .92rem; margin-bottom: 6px; color: var(--ink-soft); }
.field input, .field select {
  width: 100%; padding: 12px 14px; font-size: 1rem; font-family: inherit;
  border: 2px solid var(--line); border-radius: 14px; background: #fff; color: var(--ink); outline: none;
  transition: border-color .15s;
}
.field input:focus, .field select:focus { border-color: var(--primary); }
.field.inline { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.field.inline > span { margin: 0; }
.field.inline select { width: auto; min-width: 170px; padding: 9px 12px; }
.check { display: flex; align-items: center; gap: 8px; font-size: .92rem; color: var(--ink-soft); margin-bottom: 8px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.form-error { min-height: 1.2em; color: #c4554b; font-size: .92rem; margin: 6px 0; }

/* ---------- Menu ---------- */
.topbar { width: 100%; max-width: 980px; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.player-chip, .coin-chip {
  display: flex; align-items: center; gap: 8px; background: var(--card); padding: 6px 14px 6px 6px;
  border-radius: 999px; box-shadow: var(--shadow-sm); font-weight: 600;
}
.coin-chip { padding: 8px 14px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--sage); color: #fff; display: grid; place-items: center; font-weight: 700; text-transform: uppercase; }
.coin-ico { display: inline-block; width: 18px; height: 18px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fff3b0, #f5c542 55%, #d99a1e); box-shadow: inset 0 0 0 2px #e6a92a; vertical-align: -3px; }
.coin-ico.sm { width: 15px; height: 15px; }
.coin-chip.bump { animation: bump .35s ease; }
@keyframes bump { 50% { transform: scale(1.12); } }

.menu-main { width: 100%; max-width: 980px; margin: auto; padding: 18px 0 24px; display: flex; flex-direction: column; align-items: center; }
.mode-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; width: 100%; }
.mode-card { padding: 22px; display: flex; flex-direction: column; }
.mode-card h2 { margin: 6px 0 6px; font-size: 1.45rem; }
.mode-card p { margin: 0 0 16px; color: var(--ink-soft); line-height: 1.45; flex: 1; }
.mode-art { font-size: 2.4rem; letter-spacing: 6px; }
.mode-card.builder { background: linear-gradient(160deg, #fffaf2, #fdeee3); }
.mode-card.drive { background: linear-gradient(160deg, #fffaf2, #e9f3f8); }
.mode-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.menu-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; width: 100%; margin-top: 18px; }
.btn.tile { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 14px 8px; background: var(--card); }
.btn.tile span { font-size: 1.6rem; }
.stats-row { display: flex; gap: 12px; margin-top: 18px; width: 100%; }
.stats-row > div { flex: 1; background: rgba(255, 250, 242, .85); border-radius: 18px; padding: 12px; text-align: center; box-shadow: var(--shadow-sm); }
.stats-row b { display: block; font-size: 1.35rem; }
.stats-row span { font-size: .82rem; color: var(--muted); }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(74, 64, 88, .35); backdrop-filter: blur(3px); display: flex; align-items: center; justify-content: center; padding: 16px; animation: fadeIn .2s ease; }
.modal-card { position: relative; background: var(--card); border-radius: 26px; box-shadow: var(--shadow); width: 100%; max-width: 440px; max-height: calc(100vh - 32px); overflow-y: auto; padding: 24px; border: 2px solid #fff; }
.modal-card.wide { max-width: 760px; }
.modal-card.small { max-width: 360px; text-align: center; }
.modal-card h2 { margin: 0 0 8px; }
.modal-close { position: absolute; top: 14px; right: 14px; width: 36px; height: 36px; border-radius: 12px; border: 0; background: var(--card-2); cursor: pointer; font-size: 1rem; color: var(--ink-soft); }

.shop-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 12px; }
.shop-item { background: var(--card-2); border-radius: 18px; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.shop-item .si-head { display: flex; align-items: center; gap: 10px; }
.shop-item .si-ico { font-size: 1.6rem; width: 44px; height: 44px; display: grid; place-items: center; background: #fff; border-radius: 14px; }
.shop-item h3 { margin: 0; font-size: 1.05rem; }
.shop-item p { margin: 0; font-size: .86rem; color: var(--ink-soft); flex: 1; }
.pips { display: flex; gap: 4px; }
.pip { width: 18px; height: 8px; border-radius: 4px; background: #e6dccb; }
.pip.on { background: var(--sage); }
.shop-item .btn { padding: 9px 12px; font-size: .92rem; margin-top: 4px; }

.garage-list { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-top: 12px; }
.skin { background: var(--card-2); border-radius: 18px; padding: 12px 8px; text-align: center; border: 3px solid transparent; }
.skin.selected { border-color: var(--sage); background: #f1f8ee; }
.skin-car { position: relative; width: 78px; height: 46px; margin: 4px auto 8px; }
.skin-car .sc-roof { position: absolute; left: 18px; width: 38px; bottom: 26px; height: 16px; border-radius: 11px 12px 0 0; background: var(--c); filter: brightness(.94); }
.skin-car .sc-roof::before, .skin-car .sc-roof::after { content: ''; position: absolute; top: 4px; bottom: 0; background: #d8eef8; }
.skin-car .sc-roof::before { left: 5px; width: 13px; border-radius: 6px 2px 0 0; }
.skin-car .sc-roof::after { right: 5px; width: 13px; border-radius: 2px 6px 0 0; }
.skin-car .sc-body { position: absolute; left: 0; right: 0; bottom: 9px; height: 19px; border-radius: 9px 14px 7px 7px; background: var(--c); box-shadow: inset 0 -4px 0 rgba(0, 0, 0, .08); }
.skin-car .sc-body::before { content: ''; position: absolute; right: 3px; top: 5px; width: 6px; height: 5px; border-radius: 2px; background: #fff4c2; }
.skin-car .sc-body::after { content: ''; position: absolute; left: 2px; top: 5px; width: 4px; height: 5px; border-radius: 2px; background: #ef7d74; }
.skin-car .sc-wheel { position: absolute; bottom: 0; width: 19px; height: 19px; border-radius: 50%; background: #3d3a4a; box-shadow: inset 0 0 0 4px #3d3a4a, inset 0 0 0 7px #b8b2c4; }
.skin-car .sc-wheel.front { right: 9px; }
.skin-car .sc-wheel.rear { left: 9px; }
.skin b { display: block; font-size: .9rem; }
.skin .btn { margin-top: 8px; padding: 7px 10px; font-size: .85rem; width: 100%; }

.lb-list { list-style: none; padding: 0; margin: 6px 0 0; }
.lb-list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; margin-bottom: 6px; background: var(--card-2); }
.lb-list li.me { background: #fdeee3; outline: 2px solid var(--primary); }
.lb-rank { width: 34px; text-align: center; font-weight: 700; }
.lb-name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-val { font-weight: 700; color: var(--ink-soft); }
.lb-empty { text-align: center; color: var(--muted); padding: 24px 0; }
.lb-me { margin-top: 10px; text-align: center; color: var(--ink-soft); font-size: .95rem; }

.range { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin: 14px 0; font-weight: 500; }
.range input { flex: 1; max-width: 220px; accent-color: var(--primary); }

.howto { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.howto h3 { margin: 8px 0; }
.howto ul { margin: 0; padding-left: 20px; color: var(--ink-soft); line-height: 1.55; font-size: .95rem; }
.howto li { margin-bottom: 6px; }

.result-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0; }
.result-stats div { background: var(--card-2); border-radius: 16px; padding: 12px; }
.result-stats b { display: block; font-size: 1.4rem; }
.result-stats span { font-size: .82rem; color: var(--muted); }

/* ---------- Toasts ---------- */
#toasts { position: fixed; left: 50%; top: 76px; transform: translateX(-50%); z-index: 9999; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: rgba(255, 250, 242, .97); color: var(--ink); padding: 10px 18px; border-radius: 999px; box-shadow: var(--shadow); font-weight: 500; animation: toastIn .3s ease, toastOut .4s ease forwards 2.6s; text-align: center; }
.toast.bad { background: #fde3e0; color: #b64d43; }
.toast.good { background: #eaf6ec; color: #3f7d55; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-10px) scale(.95); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-8px); } }

/* ---------- Game HUD ---------- */
#builder-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }
.hud { position: absolute; z-index: 5; pointer-events: none; }
.hud-top { top: 0; left: 0; right: 0; height: 64px; display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; gap: 8px; }
.hud-group { display: flex; align-items: center; gap: 8px; pointer-events: auto; }
.hud-pill { display: flex; align-items: center; gap: 6px; background: rgba(255, 250, 242, .93); padding: 7px 12px; border-radius: 999px; box-shadow: var(--shadow-sm); font-size: .95rem; white-space: nowrap; }
.hud-btn { min-width: 42px; height: 40px; border: 0; border-radius: 14px; background: rgba(255, 250, 242, .95); box-shadow: var(--shadow-sm); cursor: pointer; font-weight: 700; font-size: 1rem; color: var(--ink); }
.hud-btn:active { transform: scale(.94); }
.hud-group.happy { background: rgba(255, 250, 242, .93); padding: 7px 14px; border-radius: 999px; box-shadow: var(--shadow-sm); }
.bar { width: 140px; height: 12px; border-radius: 8px; background: #eadfcd; overflow: hidden; }
.bar-fill { height: 100%; width: 100%; background: linear-gradient(90deg, #8cc29f, #b6dc8e); border-radius: 8px; transition: width .3s ease, background .3s; }
.bar-fill.warn { background: linear-gradient(90deg, #f6b26b, #f6d27a); }
.bar-fill.bad { background: linear-gradient(90deg, #e57373, #f39b8f); }

.toolbar {
  position: absolute; z-index: 5; left: 50%; bottom: 12px; transform: translateX(-50%);
  display: flex; gap: 8px; padding: 8px; background: rgba(255, 250, 242, .95); border-radius: 22px; box-shadow: var(--shadow);
  max-width: calc(100vw - 16px); overflow-x: auto;
}
.tool { position: relative; width: 72px; height: 66px; border: 0; border-radius: 16px; background: var(--card-2); cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; color: var(--ink); flex-shrink: 0; transition: transform .1s, background .15s; }
.tool .t-ico { font-size: 1.45rem; line-height: 1.1; }
.tool .t-name { font-size: .78rem; font-weight: 600; }
.tool .t-cost { font-size: .68rem; color: var(--muted); }
.tool.active { background: var(--primary); color: #fff; box-shadow: 0 4px 0 var(--primary-d); transform: translateY(-3px); }
.tool.active .t-cost { color: #ffe9e5; }
.builder-hint { position: absolute; z-index: 5; left: 50%; bottom: 96px; transform: translateX(-50%); background: rgba(74, 64, 88, .82); color: #fff; padding: 8px 16px; border-radius: 999px; font-size: .9rem; pointer-events: none; transition: opacity .6s; white-space: nowrap; max-width: calc(100vw - 24px); overflow: hidden; text-overflow: ellipsis; }
.builder-hint.hide { opacity: 0; }

/* ---------- Drive HUD ---------- */
#drive-container { position: absolute; inset: 0; }
#drive-container canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.mission { pointer-events: none; display: flex; align-items: center; gap: 10px; background: rgba(255, 250, 242, .93); padding: 7px 16px 7px 8px; border-radius: 999px; box-shadow: var(--shadow-sm); max-width: 40vw; }
.mission .arrow { width: 34px; height: 34px; border-radius: 50%; background: var(--sage); color: #fff; display: grid; place-items: center; font-size: 1.1rem; transition: transform .1s linear; flex-shrink: 0; }
.mission.drop .arrow { background: #f4a261; }
.mission b { display: block; font-size: .92rem; }
.mission span { display: block; font-size: .8rem; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#d-minimap { position: absolute; z-index: 5; right: 12px; top: 72px; width: 170px; height: 170px; border-radius: 22px; box-shadow: var(--shadow); border: 3px solid rgba(255, 250, 242, .95); background: #cfe6b8; }
.speedo { position: absolute; z-index: 5; left: 50%; bottom: 16px; transform: translateX(-50%); background: rgba(255, 250, 242, .93); border-radius: 22px; padding: 6px 22px; text-align: center; box-shadow: var(--shadow); }
.speedo b { display: block; font-size: 2rem; line-height: 1.1; }
.speedo span { font-size: .78rem; color: var(--muted); }
.drive-help { position: absolute; z-index: 5; left: 12px; bottom: 16px; background: rgba(74, 64, 88, .7); color: #fff; padding: 8px 14px; border-radius: 14px; font-size: .8rem; max-width: 280px; transition: opacity 1s; }
.drive-help.hide { opacity: 0; }
.drive-error { position: absolute; z-index: 6; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; padding: 24px; background: var(--bg); font-size: 1.05rem; flex-direction: column; gap: 14px; }
.touch-controls { position: absolute; z-index: 5; left: 0; right: 0; bottom: 84px; display: none; justify-content: space-between; padding: 0 14px; pointer-events: none; }
.touch-controls.show { display: flex; }
.tc-left, .tc-right { display: flex; gap: 12px; align-items: flex-end; pointer-events: auto; }
.tc-btn { width: 72px; height: 72px; border-radius: 50%; border: 0; background: rgba(255, 250, 242, .78); box-shadow: var(--shadow-sm); font-size: 1.5rem; color: var(--ink); touch-action: none; }
.tc-btn.small { width: 52px; height: 52px; font-size: 1.1rem; }
.tc-btn.gas { background: rgba(140, 194, 159, .9); color: #fff; }
.tc-btn.pressed { transform: scale(.92); filter: brightness(.92); }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .mode-grid { grid-template-columns: 1fr; }
  .menu-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .shop-list { grid-template-columns: 1fr; }
  .garage-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .howto { grid-template-columns: 1fr; }
  .bar { width: 70px; }
  .hud-top { height: auto; flex-wrap: wrap; padding: 8px; }
  .hud-pill { padding: 5px 9px; font-size: .85rem; }
  .tool { width: 60px; height: 58px; }
  .tool .t-ico { font-size: 1.2rem; }
  .builder-hint { bottom: 84px; font-size: .8rem; }
  .mission { max-width: 100%; order: 3; flex: 1 1 100%; }
  #d-minimap { width: 110px; height: 110px; top: 118px; }
  .drive-help { display: none; }
  .speedo { bottom: 8px; padding: 4px 16px; }
  .speedo b { font-size: 1.5rem; }
  .touch-controls { bottom: 80px; }
  .stats-row b { font-size: 1.1rem; }
}
@media (max-height: 520px) {
  .touch-controls { bottom: 12px; }
  .tc-btn { width: 60px; height: 60px; }
  .speedo { left: auto; right: 12px; transform: none; bottom: auto; top: 70px; }
  #d-minimap { display: none; }
}
