:root {
  --bg: #f5f5fb;
  --surface: #ffffff;
  --surface-2: #eef0f7;
  --text: #14142b;
  --muted: #62627a;
  --border: #e2e3ee;
  --brand: #4f46e5;
  --brand-ink: #ffffff;
  --bar: #1e1b4b;
  --bar-ink: #ffffff;
  --radius: 18px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(20 20 60 / 0.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0b0b1a;
  --surface: #15152a;
  --surface-2: #1f1f38;
  --text: #ececf6;
  --muted: #9a9ab5;
  --border: #2a2a46;
  --brand: #818cf8;
  --brand-ink: #0b0b1a;
  --bar: #12112b;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Bars ---------- */
.topbar, .appbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--bar);
  color: var(--bar-ink);
}
.topbar { justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.1rem; letter-spacing: -0.01em; }
.brand img { border-radius: 9px; }
.actions { display: flex; gap: 6px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px;
  border: 0; border-radius: 12px;
  background: rgb(255 255 255 / 0.1);
  color: inherit; cursor: pointer; text-decoration: none;
  transition: background 0.15s, transform 0.1s;
}
.icon-btn:hover { background: rgb(255 255 255 / 0.2); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn-spacer { width: 40px; }

/* ---------- Home ---------- */
.content { max-width: 760px; margin: 0 auto; padding: 20px 16px calc(32px + var(--safe-bottom)); }
.greeting { margin: 4px 0 2px; font-size: 1.7rem; font-weight: 800; letter-spacing: -0.02em; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 0.82rem; margin-top: 10px; }

.search {
  display: flex; align-items: center; gap: 8px;
  margin: 18px 0 6px; padding: 0 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  color: var(--muted);
}
.search svg { width: 18px; height: 18px; }
.search input {
  flex: 1; border: 0; outline: 0; background: transparent;
  padding: 12px 0; font: inherit; color: var(--text);
}

.section-title {
  margin: 22px 0 10px;
  font-size: 0.78rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted);
}

.app-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }

.app-card {
  position: relative;
  display: flex; gap: 14px; align-items: flex-start;
  padding: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: inherit; text-decoration: none;
  transition: transform 0.12s, border-color 0.15s;
}
.app-card:hover { border-color: var(--brand); }
.app-card:active { transform: scale(0.985); }
.app-card img { width: 56px; height: 56px; border-radius: 14px; flex-shrink: 0; }
.app-card .name { font-weight: 800; font-size: 1rem; }
.app-card .cat { font-size: 0.7rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; color: var(--brand); }
.app-card .desc { font-size: 0.83rem; color: var(--muted); margin-top: 3px; line-height: 1.4; }
.running-dot {
  position: absolute; top: 12px; right: 12px;
  width: 9px; height: 9px; border-radius: 50%;
  background: #10b981; box-shadow: 0 0 0 3px rgb(16 185 129 / 0.2);
}

.app-card.placeholder {
  border-style: dashed; box-shadow: none; background: transparent;
  align-items: center; color: var(--muted);
}
.placeholder .plus {
  width: 56px; height: 56px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2); font-size: 1.6rem;
}

.recent-row { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 4px; }
.recent-item { display: flex; flex-direction: column; align-items: center; gap: 6px; width: 72px; text-decoration: none; color: inherit; }
.recent-item img { width: 60px; height: 60px; border-radius: 16px; box-shadow: var(--shadow); }
.recent-item span { font-size: 0.72rem; text-align: center; line-height: 1.2; }

.empty { text-align: center; color: var(--muted); padding: 24px; }

/* ---------- App runner ---------- */
.view-app { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--bg); }
.view-app[hidden] { display: none; }
.appbar { position: relative; padding-bottom: 8px; }
.appbar img { border-radius: 7px; }
.appbar-title { flex: 1; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.frames { position: relative; flex: 1; }
.frames iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }

/* ---------- Settings ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card-title { margin: 0 0 12px; font-size: 1rem; font-weight: 800; }
.row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
.row:last-of-type { border-bottom: 0; }
.row span { color: var(--muted); }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.segmented button {
  border: 0; border-radius: 9px; padding: 9px; cursor: pointer;
  font: inherit; font-weight: 700; font-size: 0.88rem;
  background: transparent; color: var(--muted);
}
.segmented button[aria-checked='true'] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.btn {
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; cursor: pointer;
  font: inherit; font-weight: 700; font-size: 0.88rem;
  background: var(--surface-2); color: var(--text);
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translateX(-50%);
  z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center;
  width: min(92vw, 420px); pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-radius: 14px;
  background: var(--bar); color: var(--bar-ink);
  font-size: 0.9rem; font-weight: 600;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
  animation: toast-in 0.2s ease-out;
}
.toast button {
  border: 0; border-radius: 9px; padding: 6px 10px; cursor: pointer;
  font: inherit; font-weight: 800; background: #818cf8; color: #0b0b1a;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* Inside an app, keep toasts clear of the app's own bottom bars. */
body.in-app .toasts { bottom: auto; top: calc(70px + var(--safe-top)); }
