:root {
    --bg: #f4f6fa;
    --panel: #ffffff;
    --ink: #1c2330;
    --muted: #6b7383;
    --line: #e6e9f0;
    --accent: #2f6fed;
    --danger: #e14747;
    --tab-color: #888888;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}

button { font: inherit; cursor: pointer; }
img { max-width: 100%; display: block; user-select: none; -webkit-user-drag: none; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

.loading { padding: 24px; color: var(--muted); font-size: 18px; }

/* ===== Top bar ===== */
.bar {
    display: flex; gap: 10px; padding: 10px;
    background: var(--panel); border-bottom: 1px solid var(--line);
    align-items: stretch;
    padding-top: max(10px, env(safe-area-inset-top));
}
.sentence {
    flex: 1; min-width: 0;
    font-size: 28px; line-height: 1.2;
    padding: 10px 14px; border: 2px solid var(--line); border-radius: 12px;
    resize: none; background: #fafbff; color: var(--ink);
    outline: none;
}
.sentence:focus { border-color: var(--accent); }
.bar-actions { display: flex; gap: 8px; }
.btn {
    border: none; border-radius: 12px; padding: 10px 14px;
    font-size: 18px; font-weight: 600; color: white;
    display: flex; align-items: center; gap: 6px; min-width: 64px;
    justify-content: center;
}
.btn .ic { font-size: 22px; }
.btn .lbl { font-size: 14px; }
.btn.speak { background: var(--accent); }
.btn.clear { background: var(--danger); }
.btn.back  { background: #6b7383; }
.btn:active { transform: translateY(1px); filter: brightness(0.95); }

/* ===== Layout ===== */
.layout { flex: 1; display: flex; min-height: 0; }

/* ===== Side tabs ===== */
.tabs {
    width: 110px; flex-shrink: 0;
    background: var(--panel); border-right: 1px solid var(--line);
    overflow-y: auto;
    padding: 8px 6px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 6px;
}
.tab {
    appearance: none;
    background: white; color: var(--ink);
    border: 2px solid var(--line); border-left: 6px solid var(--tab-color);
    border-radius: 12px; padding: 10px 6px;
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    text-decoration: none;
    transition: transform 0.06s ease;
}
.tab:active { transform: translateY(1px); }
.tab.active {
    background: var(--tab-color); color: white;
    border-color: var(--tab-color);
}
.tab.active .tab-name { color: white; }
.tab-icon { font-size: 32px; line-height: 1; }
.tab-name { font-size: 13px; font-weight: 600; }
.admin-link { margin-top: auto; opacity: 0.7; }

/* ===== Board ===== */
.board { flex: 1; overflow-y: auto; padding: 14px; }
.empty { color: var(--muted); font-size: 18px; padding: 24px; text-align: center; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
}
.card {
    appearance: none; background: white; color: var(--ink);
    border: 2px solid var(--line); border-radius: 14px;
    padding: 10px; display: flex; flex-direction: column; gap: 8px;
    align-items: center;
    transition: transform 0.06s ease, border-color 0.1s ease;
}
.card:active { transform: scale(0.97); border-color: var(--accent); }
.thumb {
    width: 100%; aspect-ratio: 1;
    background: #f0f2f7; border-radius: 10px; overflow: hidden;
    display: flex; align-items: center; justify-content: center;
}
.thumb img { width: 100%; height: 100%; object-fit: contain; }
.cap {
    font-size: 18px; font-weight: 700; text-align: center;
    word-break: break-word; line-height: 1.15;
}

/* ===== iPad / phone ===== */
@media (max-width: 720px) {
    .tabs { width: 88px; padding: 6px 4px; }
    .tab-icon { font-size: 26px; }
    .tab-name { font-size: 11px; }
    .sentence { font-size: 22px; }
    .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
    .cap { font-size: 15px; }
    .btn .lbl { display: none; }
}
@media (min-width: 1100px) {
    .grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
}

/* ===== Admin styles ===== */
.admin-shell { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.admin-header {
    display: flex; align-items: center; gap: 12px; padding: 12px 16px;
    background: var(--panel); border-bottom: 1px solid var(--line);
}
.admin-header h1 { margin: 0; font-size: 20px; }
.admin-header .spacer { flex: 1; }
.admin-header a, .admin-header button { font-size: 14px; }

.admin-body { flex: 1; display: flex; min-height: 0; }
.admin-side {
    width: 240px; border-right: 1px solid var(--line); background: var(--panel);
    overflow-y: auto; padding: 10px;
}
.admin-side h3 { margin: 8px 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.admin-tab {
    display: flex; align-items: center; gap: 8px; padding: 8px 10px;
    border-radius: 10px; cursor: pointer; user-select: none;
    border: 2px solid transparent; border-left: 6px solid #ccc;
    margin-bottom: 4px; background: white;
}
.admin-tab.active { border-color: var(--accent); background: #eef3ff; }
.admin-tab.hidden-row { opacity: 0.55; }
.admin-tab .name { flex: 1; font-weight: 600; }
.admin-tab .ico { font-size: 20px; }
.admin-tab .grip { color: var(--muted); cursor: grab; }

.admin-main { flex: 1; overflow-y: auto; padding: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row + .row { margin-top: 10px; }
.input, .select {
    border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 15px;
    background: white;
}
.input.color { padding: 2px; width: 44px; height: 36px; }
.btn-sm {
    border: 1px solid var(--line); background: white; color: var(--ink);
    border-radius: 8px; padding: 6px 12px; font-size: 14px; font-weight: 600;
}
.btn-sm.primary { background: var(--accent); color: white; border-color: var(--accent); }
.btn-sm.danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-sm.ghost { background: transparent; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 14px; }
.panel h2 { margin: 0 0 10px; font-size: 16px; }

.img-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
}
.img-card {
    background: white; border: 2px solid var(--line); border-radius: 12px;
    padding: 8px; display: flex; flex-direction: column; gap: 6px;
}
.img-card.hidden-row { opacity: 0.55; }
.img-card .thumb { aspect-ratio: 1; background: #f0f2f7; border-radius: 8px; overflow: hidden; }
.img-card .thumb img { width: 100%; height: 100%; object-fit: contain; }
.img-card .label { font-weight: 700; font-size: 14px; text-align: center; }
.img-card .controls { display: flex; gap: 4px; justify-content: center; flex-wrap: wrap; }
.img-card .grip { cursor: grab; color: var(--muted); text-align: center; font-size: 18px; }

.sortable-ghost { opacity: 0.4; }
.sortable-chosen { box-shadow: 0 0 0 3px var(--accent); }

.login-wrap {
    display: flex; align-items: center; justify-content: center;
    height: 100vh; height: 100dvh; padding: 16px;
}
.login-card {
    background: white; border: 1px solid var(--line); border-radius: 16px;
    padding: 24px; width: 100%; max-width: 360px;
    box-shadow: 0 8px 30px rgba(20, 30, 60, 0.08);
}
.login-card h1 { margin: 0 0 16px; font-size: 22px; }
.login-card .input { width: 100%; margin-bottom: 10px; font-size: 16px; }
.login-card .btn-sm { width: 100%; padding: 12px; font-size: 16px; }
.err { color: var(--danger); font-size: 14px; margin-top: 8px; }

.muted { color: var(--muted); font-size: 13px; }
.flex-1 { flex: 1; }
