:root {
  --bg0: #0b0614;
  --bg1: #160b28;
  --violet: #7b3fe4;
  --violet2: #a78bfa;
  --gold: #d4af37;
  --gold2: #f0d78c;
  --ink: #f5f0ff;
  --muted: #b9a9d4;
  --card: #1c1033;
  --card-border: rgba(212, 175, 55, 0.35);
  --danger: #e8719a;
  --ok: #7ddea3;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav-h: 64px;
  font-synthesis: none;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: radial-gradient(1200px 800px at 50% -10%, #2a1048 0%, var(--bg0) 55%);
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Times New Roman", serif;
}
body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, textarea, select {
  font: inherit;
  color: inherit;
}
a { color: var(--gold2); }

.app-header {
  flex: 0 0 auto;
  padding: 14px 16px 8px;
  text-align: center;
  background: linear-gradient(180deg, rgba(11,6,20,.92), rgba(11,6,20,.4));
  border-bottom: 1px solid rgba(212,175,55,.2);
  backdrop-filter: blur(10px);
}
.app-header h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--gold2);
  font-weight: 600;
}
.app-header p {
  margin: 4px 0 0;
  font-size: .78rem;
  color: var(--muted);
  letter-spacing: .04em;
}

.views {
  flex: 1 1 auto;
  overflow: hidden;
  position: relative;
}
.view {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px calc(var(--nav-h) + var(--safe-bottom) + 12px);
}
.view.active { display: block; }

.nav {
  flex: 0 0 auto;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: rgba(12, 6, 22, .94);
  border-top: 1px solid rgba(212,175,55,.25);
  backdrop-filter: blur(12px);
}
.nav button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: .72rem;
  letter-spacing: .04em;
  cursor: pointer;
}
.nav button .ico { font-size: 1.25rem; line-height: 1; color: var(--violet2); }
.nav button.active { color: var(--gold2); }
.nav button.active .ico { color: var(--gold); }

.panel {
  background: linear-gradient(160deg, rgba(40,22,70,.85), rgba(20,10,36,.9));
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,.35), inset 0 1px 0 rgba(255,255,255,.04);
}
.panel h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
  color: var(--gold2);
  letter-spacing: .06em;
}
.panel .hint { color: var(--muted); font-size: .85rem; margin: 0 0 12px; line-height: 1.35; }

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.field {
  width: 100%;
  background: rgba(0,0,0,.28);
  border: 1px solid rgba(167,139,250,.25);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
.field:focus { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(212,175,55,.2); }
textarea.field { min-height: 72px; resize: vertical; }

.btn {
  appearance: none;
  border: 1px solid rgba(212,175,55,.55);
  background: linear-gradient(180deg, #6a35d0, #4a1fa0);
  color: var(--ink);
  padding: 12px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-size: .92rem;
  letter-spacing: .03em;
  box-shadow: 0 6px 18px rgba(90,40,180,.35);
}
.btn:active { transform: translateY(1px); }
.btn.secondary {
  background: transparent;
  border-color: rgba(167,139,250,.4);
  box-shadow: none;
  color: var(--violet2);
}
.btn.gold {
  background: linear-gradient(180deg, #e0c35a, #a8871f);
  color: #1a1028;
  border-color: #f0d78c;
  font-weight: 600;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }

.chip-group { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.chip {
  appearance: none;
  border: 1px solid rgba(167,139,250,.35);
  background: rgba(0,0,0,.25);
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: .82rem;
  cursor: pointer;
}
.chip.active {
  color: #1a1028;
  background: linear-gradient(180deg, var(--gold2), var(--gold));
  border-color: var(--gold2);
  font-weight: 600;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 10px;
  margin: 12px 0;
}
.card-face {
  aspect-ratio: 2 / 3.2;
  border-radius: 12px;
  border: 1px solid rgba(212,175,55,.45);
  background:
    radial-gradient(circle at 50% 20%, rgba(212,175,55,.2), transparent 50%),
    linear-gradient(165deg, #2b1850, #120820 70%);
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  box-shadow: 0 8px 20px rgba(0,0,0,.35);
  transition: transform .35s ease;
}
.card-face.reversed { transform: rotate(180deg); }
.card-face.reversed .card-meta { transform: rotate(180deg); }
.card-face .ornament {
  text-align: center;
  color: var(--gold);
  font-size: .7rem;
  letter-spacing: .2em;
}
.card-face .card-name {
  text-align: center;
  font-size: .78rem;
  line-height: 1.2;
  color: var(--gold2);
  font-weight: 600;
}
.card-face .card-meta {
  text-align: center;
  font-size: .68rem;
  color: var(--muted);
}
.card-face .suit-glyph {
  text-align: center;
  font-size: 1.4rem;
  color: var(--violet2);
}
.pos-label {
  font-size: .72rem;
  color: var(--gold);
  text-align: center;
  margin-bottom: 4px;
  letter-spacing: .04em;
}

.reading {
  white-space: pre-wrap;
  line-height: 1.45;
  font-size: .95rem;
  color: var(--ink);
}
.reading.empty { color: var(--muted); font-style: italic; }

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 40vh;
  padding-bottom: 8px;
}
.bubble {
  max-width: 92%;
  padding: 10px 12px;
  border-radius: 14px;
  line-height: 1.4;
  font-size: .92rem;
  white-space: pre-wrap;
}
.bubble.user {
  align-self: flex-end;
  background: linear-gradient(180deg, #5b2bbd, #3d1788);
  border: 1px solid rgba(212,175,55,.3);
  border-bottom-right-radius: 4px;
}
.bubble.assistant {
  align-self: flex-start;
  background: rgba(255,255,255,.06);
  border: 1px solid rgba(167,139,250,.25);
  border-bottom-left-radius: 4px;
}
.bubble.system {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
  border: 0;
}

.chat-compose {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  padding-top: 8px;
  background: linear-gradient(180deg, transparent, rgba(11,6,20,.95) 30%);
}
.chat-compose .field { flex: 1; }

.entry {
  border-bottom: 1px solid rgba(167,139,250,.15);
  padding: 12px 0;
}
.entry:last-child { border-bottom: 0; }
.entry h3 {
  margin: 0 0 4px;
  font-size: .98rem;
  color: var(--gold2);
}
.entry .tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.tag {
  font-size: .68rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(123,63,228,.25);
  color: var(--violet2);
  border: 1px solid rgba(167,139,250,.3);
}
.entry .summary { color: var(--muted); font-size: .85rem; margin: 0 0 6px; }
.entry .body { font-size: .9rem; line-height: 1.45; display: none; }
.entry.open .body { display: block; }
.entry .toggle {
  appearance: none; border: 0; background: none;
  color: var(--gold); font-size: .8rem; padding: 0; cursor: pointer;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .75rem;
  color: var(--muted);
  margin-top: 6px;
}
.status-pill .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--danger);
}
.status-pill.ok .dot { background: var(--ok); }

.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid rgba(240,215,140,.25);
  border-top-color: var(--gold2);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  background: rgba(20,10,36,.95);
  border: 1px solid var(--card-border);
  color: var(--ink);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: .85rem;
  z-index: 50;
  max-width: 90vw;
  display: none;
}
.toast.show { display: block; }

@media (min-width: 720px) {
  .views { max-width: 560px; margin: 0 auto; width: 100%; }
  .app-header, .nav { max-width: 560px; margin: 0 auto; width: 100%; }
}
