:root {
  --bg: #f4f1ea;
  --ink: #26221e;
  --muted: #7a7166;
  --card: #fffdf8;
  --line: #e2dccf;
  --accent: #d97757;
  --shell-a: #efe9dd;
  --shell-b: #cfc6b5;
  --glass: #151311;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1917;
    --ink: #eee8de;
    --muted: #9c9285;
    --card: #25221f;
    --line: #3a3530;
    --shell-a: #3b3631;
    --shell-b: #24201c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

.stage {
  max-width: 460px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px calc(env(safe-area-inset-bottom, 0px) + 24px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.top { width: 100%; display: flex; align-items: center; justify-content: space-between; }
.top h1 { font-size: 17px; font-weight: 650; letter-spacing: .02em; margin: 0; }
.icon-btn {
  border: 0; background: none; color: var(--muted); padding: 8px; border-radius: 50%; cursor: pointer;
}
.icon-btn:hover { color: var(--ink); background: var(--line); }

/* ---------- the charm ---------- */
.charm-wrap { position: relative; padding-top: 34px; margin-top: 4px; }
.ring {
  position: absolute; top: 0; left: 50%; width: 46px; height: 58px; translate: -50% 0;
  border: 7px solid #b9b2a5; border-radius: 50%;
  background: transparent;
  box-shadow: inset 0 1px 1px rgba(255,255,255,.5), 0 2px 3px rgba(0,0,0,.25);
}
.charm {
  --size: min(78vw, 330px);
  position: relative;
  width: var(--size); height: var(--size);
  border-radius: 50%;
  background: radial-gradient(120% 120% at 30% 20%, var(--shell-a), var(--shell-b));
  box-shadow:
    0 24px 50px -18px rgba(40,28,15,.55),
    0 2px 0 rgba(255,255,255,.5) inset,
    0 -6px 12px rgba(0,0,0,.18) inset;
  display: grid; place-items: center;
  transition: transform .25s cubic-bezier(.3,1.6,.5,1);
  user-select: none;
}
.charm.bump { transform: scale(.97); }
.screen {
  position: relative;
  width: 74%; height: 74%;
  border-radius: 50%;
  background: #F3EBDC;
  box-shadow: 0 0 0 5px #2b2233, 0 0 0 7px rgba(255,255,255,.12);
  overflow: hidden;
  cursor: pointer;
}
.screen::after { /* glass reflection */
  content: ""; position: absolute; inset: 0; border-radius: 50%; pointer-events: none;
  background: linear-gradient(150deg, rgba(255,255,255,.10) 0%, rgba(255,255,255,0) 38%);
}
#out { width: 100%; height: 100%; display: block; }
.p5Canvas { display: none !important; }
.screen::before { /* inner glass shadow */
  content: ""; position: absolute; inset: 0; border-radius: 50%; pointer-events: none; z-index: 1;
  box-shadow: inset 0 6px 18px rgba(43,34,51,.35);
}

.bubble {
  position: absolute; left: 15%; right: 15%; top: 8%;
  z-index: 2;
  max-height: 27%;
  overflow: hidden;
  padding: 5px 10px 6px;
  color: #2b2233;
  font: 700 clamp(14px, 4.1vw, 16.5px)/1.1 "Gaegu", "Shantell Sans", ui-rounded, cursive;
  text-align: center;
  background: rgba(255, 245, 226, .9);
  border: 2px solid #2b2233;
  border-radius: 16px 16px 16px 16px / 14px 18px 14px 18px;   /* a little hand-drawn wobble */
  box-shadow: 2px 3px 0 rgba(43,34,51,.35);
  display: flex; flex-direction: column; justify-content: flex-end;
}
.bubble::after { /* tail */
  content: ""; position: absolute; left: 50%; bottom: -9px; width: 14px; height: 14px; translate: -50% 0; rotate: 45deg;
  background: rgba(255, 245, 226, .95); border-right: 2px solid #2b2233; border-bottom: 2px solid #2b2233;
}
.bubble[hidden] { display: none !important; }
.status {
  position: absolute; bottom: 11%; left: 0; right: 0; text-align: center;
  font: 600 10.5px/1 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .14em;
  color: rgba(43,34,51,.55); text-transform: uppercase; z-index: 2;
}
.screen.night .status { color: rgba(255,245,226,.6);
}

.sensor {
  position: absolute; left: 74.6%; top: 74.6%;
  width: 13%; aspect-ratio: 1;
  border-radius: 50%; border: 0; cursor: pointer;
  background: radial-gradient(circle at 40% 35%, #d9d2c4, #a79e8e);
  color: #6f675b;
  box-shadow: inset 0 2px 3px rgba(0,0,0,.35), 0 1px 0 rgba(255,255,255,.6);
  display: grid; place-items: center; padding: 8px;
  touch-action: none;
  transition: color .2s, box-shadow .2s;
}
.sensor svg { width: 100%; height: 100%; }
.sensor.on {
  color: var(--accent);
  box-shadow: inset 0 2px 3px rgba(0,0,0,.35), 0 0 0 3px color-mix(in srgb, var(--accent) 55%, transparent), 0 0 22px var(--accent);
}
@media (prefers-color-scheme: dark) {
  .sensor { background: radial-gradient(circle at 40% 35%, #5b544c, #37322c); color: #a39888; box-shadow: inset 0 2px 3px rgba(0,0,0,.5), 0 1px 0 rgba(255,255,255,.08); }
  .ring { border-color: #6d665c; }
}

.hint { margin: -4px 0 0; color: var(--muted); font-size: 13px; text-align: center; min-height: 1.5em; }

/* ---------- input ---------- */
.talk {
  width: 100%; display: flex; gap: 8px;
  background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 5px 5px 5px 16px;
}
.talk input { flex: 1; min-width: 0; border: 0; background: none; color: inherit; font: inherit; outline: none; }
.send {
  border: 0; border-radius: 50%; width: 38px; height: 38px; cursor: pointer;
  background: var(--accent); color: #fff; display: grid; place-items: center;
}
.send:disabled { opacity: .45; cursor: default; }

.log { width: 100%; font-size: 14px; }
.log summary { color: var(--muted); cursor: pointer; font-size: 13px; }
.log ol { list-style: none; margin: 10px 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow: auto; }
.log li { padding: 7px 12px; border-radius: 14px; max-width: 85%; overflow-wrap: anywhere; }
.log li.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.log li.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.link { border: 0; background: none; color: var(--muted); text-decoration: underline; cursor: pointer; font: inherit; font-size: 12.5px; padding: 0; }

.foot { color: var(--muted); font-size: 11.5px; text-align: center; line-height: 1.6; max-width: 380px; }
.foot a { color: inherit; }

/* ---------- settings ---------- */
dialog {
  border: 0; padding: 0; background: var(--card); color: var(--ink);
  width: min(460px, 100vw); max-height: 92dvh; border-radius: 20px;
  box-shadow: 0 30px 80px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(20,16,12,.45); backdrop-filter: blur(3px); }
.sheet { padding: 20px 18px 18px; display: flex; flex-direction: column; gap: 14px; }
.sheet h2 { margin: 0; font-size: 18px; }
fieldset { border: 1px solid var(--line); border-radius: 14px; margin: 0; padding: 10px 14px 14px; display: flex; flex-direction: column; gap: 10px; }
legend { color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: .06em; padding: 0 6px; }
.sheet label, .field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.sheet label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
.sheet input:not([type]), .sheet input[type=password], .sheet textarea, .sheet select {
  font: inherit; font-size: 14.5px; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; width: 100%;
}
.sheet textarea { resize: vertical; }
.sheet input[type=range] { accent-color: var(--accent); }
.note { margin: -4px 0 0; font-size: 12px; color: var(--muted); }
.skins { display: flex; gap: 8px; flex-wrap: wrap; }
.skins button {
  width: 60px; height: 60px; border-radius: 14px; border: 2px solid var(--line); background: #F3EBDC; cursor: pointer; padding: 2px;
}
.skins button[aria-pressed=true] { border-color: var(--accent); }
.skins img { width: 100%; height: 100%; display: block; border-radius: 10px; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatches button { width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; box-shadow: 0 0 0 1px var(--line); }
.swatches button[aria-pressed=true] { border-color: var(--card); box-shadow: 0 0 0 2px var(--ink); }
.sheet-actions { display: flex; justify-content: flex-end; }
.primary { border: 0; background: var(--accent); color: #fff; font: inherit; font-weight: 600; padding: 9px 22px; border-radius: 999px; cursor: pointer; }

.badge { font-size: 11px; font-weight: 600; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 2px 8px; margin-left: 6px; vertical-align: 2px; }
.offer {
  width: 100%; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px 12px;
  background: var(--card); border: 1px dashed var(--accent); border-radius: 14px; padding: 10px 14px; font-size: 13.5px; text-align: center;
}
.offer-actions { display: inline-flex; gap: 12px; align-items: center; }
.primary.small { padding: 6px 14px; font-size: 13px; }
#btn-local { align-self: flex-start; }
#btn-local:disabled { opacity: .5; }
progress { width: 100%; accent-color: var(--accent); }
