:root {
  color-scheme: dark;
  font-family: Inter, ui-rounded, "Segoe UI", "Microsoft YaHei UI", system-ui, sans-serif;
  --panel: #111a2b;
  --panel-soft: #17233a;
  --line: rgba(255, 255, 255, .1);
  --text: #f6f8ff;
  --muted: #9aa9c5;
  --accent: #f3ad36;
  --accent-2: #ffd882;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; min-height: 100dvh; margin: 0; overflow: hidden; }
body { background: #0b1220; color: var(--text); }
button, select, input { font: inherit; }
button, select { color: inherit; }
button { cursor: pointer; }

.app-shell { display: grid; grid-template-rows: 70px 1fr; height: 100%; min-height: 100dvh; }
.topbar {
  z-index: 20; display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px; background: rgba(10, 17, 30, .94); border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 14px;
  color: #172033; font-weight: 900; font-size: 22px;
  background: linear-gradient(145deg, var(--accent-2), var(--accent)); box-shadow: 0 8px 24px rgba(243, 173, 54, .2);
}
.brand strong, .brand small { display: block; }
.brand small { margin-top: 2px; color: var(--muted); font-size: 12px; }
.top-actions, .quick-actions, .action-picker { display: flex; gap: 8px; }
button { border: 0; border-radius: 11px; padding: 10px 14px; font-weight: 700; transition: .18s ease; }
button:hover { transform: translateY(-1px); filter: brightness(1.08); }
button:active { transform: translateY(0); }
button.primary { background: var(--accent); color: #1b2130; }
button.ghost { background: var(--panel-soft); border: 1px solid var(--line); }

.workspace { position: relative; min-height: 0; display: grid; grid-template-columns: 1fr 360px; }
.workspace.panel-closed { grid-template-columns: 1fr 0; }
.stage { position: relative; overflow: hidden; min-width: 0; touch-action: none; transition: background .25s ease; }
.background-desk {
  background-color: #10192a;
  background-image: radial-gradient(circle at 18% 22%, rgba(46, 116, 181, .3), transparent 30%),
                    radial-gradient(circle at 73% 18%, rgba(243, 173, 54, .16), transparent 23%),
                    linear-gradient(145deg, #17233a, #090f1b 75%);
}
.background-sky { background: linear-gradient(165deg, #a8d9ff, #e7f6ff 62%, #bcd6e9); }
.background-paper { background-color: #e7d2aa; background-image: repeating-linear-gradient(0deg, rgba(92, 62, 31, .04) 0 1px, transparent 1px 5px); }
.background-checker { background-color: #f5f5f5; background-image: linear-gradient(45deg, #d9d9d9 25%, transparent 25%), linear-gradient(-45deg, #d9d9d9 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #d9d9d9 75%), linear-gradient(-45deg, transparent 75%, #d9d9d9 75%); background-size: 24px 24px; background-position: 0 0, 0 12px, 12px -12px, -12px 0; }
.background-sky .stage-hint, .background-paper .stage-hint, .background-checker .stage-hint,
.background-sky .stage-status, .background-paper .stage-status, .background-checker .stage-status { color: #27334a; background: rgba(255,255,255,.72); }
.stage::after { content: ""; position: absolute; inset: auto 0 0; height: 18%; pointer-events: none; background: linear-gradient(transparent, rgba(0,0,0,.16)); }
.stage-hint, .stage-status {
  position: absolute; z-index: 4; color: rgba(255,255,255,.72); background: rgba(7,13,24,.35);
  border: 1px solid rgba(255,255,255,.08); backdrop-filter: blur(8px); font-size: 12px;
}
.stage-hint { top: 18px; left: 50%; transform: translateX(-50%); padding: 8px 13px; border-radius: 99px; white-space: nowrap; }
.stage-status { left: 16px; bottom: 15px; display: flex; gap: 12px; padding: 7px 10px; border-radius: 9px; }

.pet { position: absolute; z-index: 5; width: 320px; height: 320px; outline: none; cursor: grab; user-select: none; touch-action: none; transform-origin: 50% 85%; will-change: left, top, transform; }
.pet.dragging { cursor: grabbing; filter: drop-shadow(0 18px 18px rgba(0,0,0,.22)); }
.pet:focus-visible { border-radius: 42%; box-shadow: 0 0 0 3px var(--accent); }
#petCanvas { display: block; width: 100%; height: 100%; pointer-events: none; filter: drop-shadow(0 12px 10px rgba(0,0,0,.25)); }
.speech-bubble {
  position: absolute; z-index: 8; top: 11%; left: calc(100% + 14px); width: max-content;
  min-width: 150px; max-width: min(290px, 38vw); padding: 13px 16px; border-radius: 18px;
  background: #fff; color: #172033; font-size: 15px; font-weight: 700; line-height: 1.45;
  box-shadow: 0 12px 35px rgba(0,0,0,.22); overflow-wrap: anywhere; pointer-events: none;
  animation: bubble-in .18s ease-out;
}
.speech-bubble::before { content: ""; position: absolute; left: -10px; top: 28px; border-width: 9px 11px 9px 0; border-style: solid; border-color: transparent #fff transparent transparent; }
.speech-bubble[data-side="left"] { left: auto; right: calc(100% + 14px); }
.speech-bubble[data-side="left"]::before { left: auto; right: -10px; border-width: 9px 0 9px 11px; border-color: transparent transparent transparent #fff; }
.speech-bubble[data-side="above"] { top: auto; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); max-width: min(290px, 72vw); }
.speech-bubble[data-side="above"]::before { left: calc(50% - 9px); top: auto; bottom: -10px; border-width: 11px 9px 0; border-color: #fff transparent transparent; }
.loading-badge { position: absolute; z-index: 7; left: 50%; bottom: 8%; transform: translateX(-50%); padding: 7px 10px; white-space: nowrap; border-radius: 99px; background: rgba(12,20,34,.82); font-size: 11px; }
@keyframes bubble-in { from { opacity: 0; margin-top: 5px; } }

.control-panel { z-index: 10; overflow-y: auto; padding: 22px; background: var(--panel); border-left: 1px solid var(--line); transition: transform .25s ease, opacity .2s ease; }
.panel-closed .control-panel { transform: translateX(100%); opacity: 0; visibility: hidden; pointer-events: none; }
.panel-heading, .section-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel-heading small { color: var(--accent); letter-spacing: .14em; font-weight: 900; }
.panel-heading h1 { margin: 4px 0 0; font-size: 21px; }
.icon-button { display: none; width: 36px; height: 36px; padding: 0; background: var(--panel-soft); font-size: 23px; }
.control-section { padding: 18px 0; border-bottom: 1px solid var(--line); }
.control-section h2 { margin: 0 0 13px; font-size: 14px; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin: 11px 0; }
.toggle-row span, .toggle-row strong, .toggle-row small { display: block; }
.toggle-row strong { font-size: 13px; }
.toggle-row small, .microcopy, footer { color: var(--muted); font-size: 11px; line-height: 1.45; }
.toggle-row input { position: absolute; opacity: 0; }
.toggle-row i { flex: 0 0 auto; position: relative; width: 38px; height: 22px; border-radius: 99px; background: #33405a; transition: .2s; }
.toggle-row i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .2s; }
.toggle-row input:checked + i { background: var(--accent); }
.toggle-row input:checked + i::after { transform: translateX(16px); }
.two-column { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.two-column label { min-width: 0; color: var(--muted); font-size: 11px; }
.two-column label > span, .two-column output { display: inline-block; margin-bottom: 6px; }
.two-column output { float: right; }
select { width: 100%; min-width: 0; height: 38px; padding: 0 32px 0 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-soft); }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.section-title h2 { margin-bottom: 0; }
.section-title span { padding: 3px 8px; border-radius: 99px; background: var(--panel-soft); color: var(--accent-2); font-size: 11px; }
.action-picker { margin-top: 12px; }
.action-picker select { flex: 1; }
.quick-actions { display: grid; grid-template-columns: 1fr 1fr; }
.quick-actions button { padding: 9px 6px; font-size: 11px; }
footer { display: flex; justify-content: space-between; gap: 10px; padding-top: 18px; }
footer a { color: var(--accent-2); }

.install-tip {
  position: fixed; z-index: 40; left: max(18px, env(safe-area-inset-left)); bottom: max(18px, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 12px; max-width: min(520px, calc(100vw - 36px));
  padding: 13px 14px 13px 18px; border: 1px solid rgba(255,255,255,.14); border-radius: 16px;
  background: rgba(17,26,43,.97); box-shadow: 0 16px 48px rgba(0,0,0,.38); backdrop-filter: blur(18px);
}
.install-tip[hidden] { display: none; }
.install-tip span { flex: 1; font-size: 13px; line-height: 1.45; }
.install-tip .install-tip-close { display: grid; place-items: center; flex: 0 0 36px; }

@media (max-width: 850px) {
  .workspace { display: block; height: 100%; overflow: hidden; }
  .stage { width: 100%; height: 100%; }
  .control-panel { position: absolute; inset: 0 0 0 auto; width: min(370px, 92vw); box-shadow: -18px 0 40px rgba(0,0,0,.35); }
  .panel-closed .control-panel { transform: translateX(105%); }
  .icon-button { display: block; }
}
@media (max-width: 640px) {
  html, body, .app-shell, .workspace, .stage, .topbar { width: 100vw; max-width: 100vw; min-width: 0; }
  .app-shell { grid-template-rows: calc(60px + env(safe-area-inset-top)) 1fr; }
  .topbar { position: relative; padding-top: env(safe-area-inset-top); padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .brand { min-width: 0; max-width: calc(100vw - 170px); }
  .brand strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top-actions { position: absolute; right: max(12px, env(safe-area-inset-right)); top: calc(10px + env(safe-area-inset-top)); }
  #fullscreenButton { display: none !important; }
  .control-panel {
    inset: auto 0 0; width: 100%; max-height: min(78dvh, 720px);
    padding: 18px max(18px, env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
    border-left: 0; border-top: 1px solid var(--line); border-radius: 22px 22px 0 0;
    box-shadow: 0 -18px 40px rgba(0,0,0,.35);
  }
  .panel-closed .control-panel { transform: translateY(105%); }
  .panel-heading { position: sticky; top: -18px; z-index: 2; margin: -18px -18px 0; padding: 18px; background: rgba(17,26,43,.97); border-radius: 22px 22px 0 0; }
  .stage-status { left: max(12px, env(safe-area-inset-left)); bottom: max(12px, env(safe-area-inset-bottom)); }
  .stage-hint { width: calc(100% - 24px); max-width: 420px; white-space: normal; text-align: center; line-height: 1.35; }
  button, select { min-height: 44px; }
  .toggle-row { min-height: 44px; }
  .toggle-row i { width: 44px; height: 26px; }
  .toggle-row i::after { width: 20px; height: 20px; }
  .toggle-row input:checked + i::after { transform: translateX(18px); }
  .speech-bubble { max-width: min(290px, 82vw); }
  .install-tip { right: max(12px, env(safe-area-inset-right)); bottom: max(12px, env(safe-area-inset-bottom)); left: max(12px, env(safe-area-inset-left)); }
}
@media (max-width: 560px) {
  .topbar { padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left)); }
  .brand small, #fullscreenButton { display: none; }
  .brand strong { font-size: 14px; }
  .top-actions button { padding: 8px 10px; font-size: 12px; }
  .stage-hint { top: 10px; max-width: 90%; overflow: hidden; text-overflow: ellipsis; }
  .two-column { grid-template-columns: 1fr; }
  .speech-bubble { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
