:root {
  --accent: #2563eb;
  --ic: 68px;
  --text: #0f172a;
  --muted: #475569;
  --panel: rgba(255, 255, 255, .72);
  --panel-solid: #ffffff;
  --line: rgba(15, 23, 42, .12);
  --field: #f8fafc;
  --shadow: 0 10px 30px rgba(15, 23, 42, .12);
  --label: #0f172a;
  --label-shadow: none;
  --page: #eef2f9;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --text: #e8eefc;
  --muted: #94a3b8;
  --panel: rgba(15, 23, 42, .62);
  --panel-solid: #111a2e;
  --line: rgba(255, 255, 255, .12);
  --field: #0b1324;
  --shadow: 0 20px 50px rgba(0, 0, 0, .45);
  --label: #f1f5ff;
  --label-shadow: 0 1px 3px rgba(0, 0, 0, .6);
  --page: #0b1220;
  color-scheme: dark;
}
:root[data-size="s"] { --ic: 54px; }
:root[data-size="l"] { --ic: 86px; }


* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Sinhala", Arial, sans-serif;
  color: var(--text);
  background: var(--page);
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* ── background ───────────────────────── */
.bg { position: fixed; inset: 0; z-index: -1; background: var(--page); }
:root[data-bg="aurora"] .bg {
  background:
    radial-gradient(60vmax 60vmax at 10% -10%, color-mix(in srgb, var(--accent) 45%, transparent), transparent 60%),
    radial-gradient(50vmax 50vmax at 110% 10%, rgba(236, 72, 153, .30), transparent 60%),
    radial-gradient(60vmax 60vmax at 50% 120%, rgba(16, 185, 129, .25), transparent 60%),
    var(--page);
}
:root[data-bg="ocean"] .bg  { background: linear-gradient(160deg, #0ea5e9 0%, #1e3a8a 55%, #0b1220 100%); }
:root[data-bg="sunset"] .bg { background: linear-gradient(160deg, #f97316 0%, #db2777 50%, #4c1d95 100%); }
:root[data-bg="forest"] .bg { background: linear-gradient(160deg, #22c55e 0%, #0f766e 50%, #052e2b 100%); }
:root[data-bg="ocean"], :root[data-bg="sunset"], :root[data-bg="forest"] { --label: #fff; --label-shadow: 0 1px 3px rgba(0,0,0,.55); }

/* ── top bar ──────────────────────────── */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--panel);
  backdrop-filter: blur(18px) saturate(1.4);
  -webkit-backdrop-filter: blur(18px) saturate(1.4);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; margin-right: auto; }
.brand img { border-radius: 10px; flex: none; }
.brand h1 { margin: 0; font-size: 18px; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p { margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand p:empty { display: none; }

.search {
  flex: 1 1 260px; max-width: 460px; order: 2;
  display: flex; align-items: center; gap: 8px;
  background: var(--field); border: 1px solid var(--line); border-radius: 999px;
  padding: 0 14px; height: 40px; color: var(--muted);
}
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 15px; }
.actions { display: flex; gap: 6px; order: 3; }
@media (max-width: 640px) {
  .search { order: 4; flex-basis: 100%; max-width: none; }
  .btn span { display: none; }
}

/* ── buttons ──────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel-solid); color: var(--text);
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn:hover { border-color: color-mix(in srgb, var(--accent) 60%, var(--line)); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.ghost { background: transparent; }
.btn.icon { width: 38px; padding: 0; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: #dc2626; }
.btn.small { height: 32px; font-size: 13px; padding: 0 10px; }
.btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ── edit bar ─────────────────────────── */
.editbar {
  position: sticky; top: 61px; z-index: 9;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin: 12px auto 0; max-width: 1200px; width: calc(100% - 32px);
  padding: 10px 14px; border-radius: 14px;
  background: color-mix(in srgb, var(--accent) 16%, var(--panel-solid));
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  font-size: 14px;
}
.editbar span { margin-right: auto; }

/* ── grid ─────────────────────────────── */
main { max-width: 1200px; margin: 0 auto; padding: 18px 16px 60px; }
.group { margin-bottom: 22px; }
.group h2 {
  margin: 0 0 10px 6px; font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--label); text-shadow: var(--label-shadow); opacity: .8;
}
.apps {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(calc(var(--ic) + 30px), 1fr));
  gap: 18px 8px;
  min-height: calc(var(--ic) + 20px);
}
.tile {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  padding: 6px 2px; border-radius: 16px;
  text-decoration: none; color: var(--label);
  cursor: pointer; user-select: none; -webkit-user-select: none;
  transition: transform .15s ease;
  outline: none;
}
.tile:hover .ic, .tile:focus-visible .ic { transform: translateY(-3px) scale(1.04); box-shadow: 0 12px 26px rgba(0,0,0,.28); }
.tile:focus-visible .ic { outline: 3px solid var(--accent); outline-offset: 3px; }
.tile:active .ic { transform: scale(.95); }
.ic {
  --c: #2563eb;
  width: var(--ic); height: var(--ic); flex: none;
  display: grid; place-items: center;
  border-radius: 23%;
  background: linear-gradient(145deg, color-mix(in srgb, var(--c) 78%, #fff) 0%, var(--c) 55%, color-mix(in srgb, var(--c) 75%, #000) 100%);
  color: #fff; font-weight: 800; font-size: calc(var(--ic) * .44);
  box-shadow: 0 6px 16px rgba(0,0,0,.18), inset 0 1px 0 rgba(255,255,255,.25);
  transition: transform .18s ease, box-shadow .18s ease;
  overflow: hidden;
}
.ic.emoji { font-size: calc(var(--ic) * .5); font-weight: 400; line-height: 1; }
.ic.text { font-size: calc(var(--ic) * .28); letter-spacing: -.02em; }
.ic.img { background: #fff; }
.ic.img img { width: 100%; height: 100%; object-fit: cover; }
.ic.img.fav img { width: 62%; height: 62%; object-fit: contain; }
.lbl {
  max-width: 100%; font-size: 12.5px; font-weight: 500; line-height: 1.25; text-align: center;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  text-shadow: var(--label-shadow); overflow-wrap: anywhere;
}
:root[data-labels="off"] .tile .lbl { display: none; }

/* edit mode */
body.editing .tile .ic { animation: jiggle .32s ease-in-out infinite alternate; }
body.editing .tile:nth-child(2n) .ic { animation-delay: -.16s; }
@keyframes jiggle { from { transform: rotate(-1.6deg); } to { transform: rotate(1.6deg); } }
@media (prefers-reduced-motion: reduce) { body.editing .tile .ic { animation: none; } }
.tile.is-hidden .ic { opacity: .35; filter: grayscale(1); }
.tile.is-hidden .lbl { opacity: .55; text-decoration: line-through; }
.tile .eye {
  position: absolute; top: -2px; right: calc(50% - var(--ic) / 2 - 10px);
  width: 26px; height: 26px; border-radius: 50%; border: 0;
  display: grid; place-items: center; cursor: pointer;
  background: var(--panel-solid); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.3);
  font-size: 13px; line-height: 1; z-index: 2;
}
.tile .eye svg { width: 15px; height: 15px; }
.tile.add .ic { background: transparent; border: 2px dashed color-mix(in srgb, var(--label) 50%, transparent); color: var(--label); box-shadow: none; animation: none !important; font-weight: 300; }
body.editing .tile { -webkit-touch-callout: none; cursor: grab; }
.tile.dragging { opacity: .25; }
.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; margin: 0;
  filter: drop-shadow(0 16px 24px rgba(0,0,0,.35));
}
.drag-ghost .ic { animation: none !important; }
body.is-dragging, body.is-dragging * { cursor: grabbing !important; user-select: none; }

.empty { text-align: center; color: var(--label); text-shadow: var(--label-shadow); padding: 40px 16px; }
.offline {
  position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #334155; color: #fff; padding: 8px 14px; border-radius: 999px; font-size: 13px;
}

/* ── dialogs ──────────────────────────── */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--line); border-radius: 18px;
  background: var(--panel-solid); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(2, 6, 23, .55); backdrop-filter: blur(3px); }
dialog form { padding: 20px; display: grid; gap: 14px; }
dialog h2 { margin: 0; font-size: 19px; }
label { display: grid; gap: 5px; font-size: 13px; font-weight: 600; color: var(--muted); }
dialog input:not([type=checkbox]):not([type=color]):not([type=file]), dialog select {
  width: 100%; height: 40px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--field); color: var(--text); font: inherit; font-size: 15px; font-weight: 400;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
input[type=color] { width: 48px; height: 40px; padding: 2px; border: 1px solid var(--line); border-radius: 10px; background: var(--field); cursor: pointer; flex: none; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.checks { display: grid; gap: 8px; }
.checks label, label.inline { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); font-size: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 520px) { .grid2 { grid-template-columns: 1fr; } }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.appform { display: grid; grid-template-columns: 110px 1fr; gap: 14px; align-items: start; }
@media (max-width: 480px) { .appform { grid-template-columns: 1fr; } .preview { justify-self: center; } }
.preview { padding: 10px 4px; border-radius: 14px; background: var(--field); border: 1px solid var(--line); --label: var(--text); --label-shadow: none; }
.tile.static { cursor: default; }
fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px 12px; display: grid; gap: 10px; margin: 0; }
legend { font-size: 13px; font-weight: 700; color: var(--muted); padding: 0 4px; }
#emojiPick button { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--field); font-size: 18px; cursor: pointer; padding: 0; }
#emojiPick button:hover { border-color: var(--accent); }
details { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
details[open] { display: grid; gap: 10px; }
summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
.err { margin: 0; color: #dc2626; font-size: 13px; min-height: 1em; }
.err:empty { display: none; }
.dlg-actions { display: flex; gap: 8px; align-items: center; }
.spacer { flex: 1; }
.steps { margin: 0; padding-left: 18px; display: grid; gap: 10px; font-size: 14px; line-height: 1.45; }
.kbd { display: inline-block; padding: 0 6px; border: 1px solid var(--line); border-radius: 6px; }
code { font-size: 12.5px; background: var(--field); padding: 1px 5px; border-radius: 5px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3); transition: .25s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: #b91c1c; }

/* standalone (installed) app tweaks */
@media (display-mode: standalone) { #btnInstall { display: none !important; } }

/* phones: 4 icons per row like a home screen */
@media (max-width: 480px) {
  :root { --ic: 58px; }
  :root[data-size="s"] { --ic: 48px; }
  :root[data-size="l"] { --ic: 72px; }
  .apps { grid-template-columns: repeat(auto-fill, minmax(calc(var(--ic) + 14px), 1fr)); gap: 16px 4px; }
  main { padding-left: 12px; padding-right: 12px; }
}
