:root {
    --bg: var(--tg-theme-bg-color, #f4f4f5);
    --text: var(--tg-theme-text-color, #111827);
    --hint: var(--tg-theme-hint-color, #6b7280);
    --card: var(--tg-theme-secondary-bg-color, #ffffff);
    --accent: var(--tg-theme-button-color, #2481cc);
    --accent-text: var(--tg-theme-button-text-color, #ffffff);
    --line: rgba(127, 127, 127, .22);
    --shadow: 0 8px 32px rgba(0, 0, 0, .14);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="dark"] {
    --line: rgba(255, 255, 255, .16);
    --shadow: 0 8px 32px rgba(0, 0, 0, .45);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    user-select: none;
    -webkit-user-select: none;
}

#app {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    padding-top: var(--safe-top);
}

.topbar {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 14px 8px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
}

.brand-icon {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: var(--card);
    box-shadow: 0 2px 8px rgba(0, 0, 0, .06);
    font-size: 21px;
}

.brand strong {
    display: block;
    font-size: 16px;
    line-height: 1.1;
}

.brand small {
    display: block;
    margin-top: 2px;
    color: var(--hint);
    font-size: 12px;
}

.word-badge {
    max-width: 58vw;
    overflow: hidden;
    padding: 9px 13px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    font-size: 16px;
    font-weight: 700;
    letter-spacing: .08em;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    margin: 0 10px;
    overflow: hidden;
    border-radius: 18px;
    background: #fff;
    box-shadow: var(--shadow);
}

#pad {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
    cursor: crosshair;
}

.stage-hint {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 1;
    width: 80%;
    transform: translate(-50%, -50%);
    color: #9ca3af;
    font-size: 17px;
    text-align: center;
    pointer-events: none;
    transition: opacity .18s ease;
}

.stage-hint.is-hidden {
    opacity: 0;
}

.panel {
    flex: 0 0 auto;
    padding: 10px 12px calc(10px + var(--safe-bottom));
}

.panel-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.palette {
    overflow-x: auto;
    padding: 2px 0 8px;
    scrollbar-width: none;
}

.palette::-webkit-scrollbar {
    display: none;
}

.swatch {
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 2px solid transparent;
    border-radius: 50%;
    background: var(--swatch);
    box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .35);
    transition: transform .12s ease, border-color .12s ease;
}

.swatch.is-active {
    transform: scale(1.1);
    border-color: var(--text);
}

.controls {
    flex-wrap: wrap;
}

.thickness {
    display: flex;
    flex: 1 1 150px;
    align-items: center;
    gap: 8px;
    min-width: 140px;
    height: 42px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    color: var(--hint);
    font-size: 12px;
    font-weight: 600;
}

.thickness input {
    flex: 1;
    accent-color: var(--accent);
}

.thickness output {
    width: 18px;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.tool {
    flex: 0 0 auto;
    height: 42px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    color: var(--text);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
}

.tool.is-active {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent);
}

.send {
    width: 100%;
    height: 52px;
    margin-top: 10px;
    border: 0;
    border-radius: 15px;
    background: var(--accent);
    color: var(--accent-text);
    font: inherit;
    font-size: 17px;
    font-weight: 700;
    box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 35%, transparent);
    transition: transform .1s ease, opacity .15s ease;
}

.send:active {
    transform: scale(.985);
}

.send:disabled {
    opacity: .55;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: grid;
    place-items: center;
    padding: 24px;
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(6px);
}

.overlay.is-hidden {
    display: none;
}

.overlay-card {
    width: min(340px, 100%);
    padding: 22px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--card);
    box-shadow: var(--shadow);
    text-align: center;
}

.overlay-card p {
    margin: 14px 0 0;
    color: var(--text);
    font-size: 15px;
    line-height: 1.45;
}

.spinner {
    width: 34px;
    height: 34px;
    margin: 0 auto;
    border: 3px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}

.overlay.is-error .spinner {
    display: none;
}

.retry {
    margin-top: 16px;
    padding: 11px 18px;
    border: 0;
    border-radius: 12px;
    background: var(--accent);
    color: var(--accent-text);
    font: inherit;
    font-weight: 700;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.toast {
    position: fixed;
    right: 14px;
    bottom: calc(14px + var(--safe-bottom));
    left: 14px;
    z-index: 30;
    padding: 12px 14px;
    border-radius: 13px;
    background: rgba(17, 24, 39, .94);
    color: #fff;
    font-size: 14px;
    line-height: 1.35;
    text-align: center;
    opacity: 0;
    transform: translateY(10px);
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
}

.toast.is-visible {
    opacity: 1;
    transform: translateY(0);
}

@media (max-height: 620px) {
    .topbar {
        padding-top: 6px;
        padding-bottom: 4px;
    }

    .brand-icon {
        width: 32px;
        height: 32px;
        font-size: 18px;
    }

    .palette {
        padding-bottom: 5px;
    }

    .swatch {
        width: 30px;
        height: 30px;
    }

    .send {
        height: 46px;
        margin-top: 7px;
    }
}
