:root {
    --bg-panel: rgba(20, 22, 28, 0.62);
    --bg-panel-solid: rgba(28, 30, 38, 0.92);
    --border: rgba(255, 255, 255, 0.14);
    --text: rgba(255, 255, 255, 0.92);
    --text-dim: rgba(255, 255, 255, 0.55);
    --accent: #4ea1ff;
    --danger: #ff4d5e;
    --radius: 16px;
    --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
    --quickbar-h: 56px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
            "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* {
    font-family: var(--font);
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
    background: #0c0d11;
}

body {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
    touch-action: none;
}

canvas {
    position: fixed;
    inset: 0;
    z-index: 0;
    display: block;
}

/* full-screen overlay container; lets clicks outside the panel reach the canvas */
#ui {
    position: fixed;
    inset: 0;
    z-index: 10;
    pointer-events: none;
}

/* ---- brand: logo + site title (top-left) ----
   pointer-events stays off so it never blocks painting on the canvas below */
#brand {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 12;
    pointer-events: none;

    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px 8px 10px;

    color: var(--text);
    background: var(--bg-panel);
    -webkit-backdrop-filter: blur(14px);
    backdrop-filter: blur(14px);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

#brand-logo {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background-size: contain;
    background-repeat: no-repeat;
}

#brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

#brand-title {
    font-size: 15px;
    font-weight: 700;
    letter-spacing: 0.3px;
}

#brand-subtitle {
    font-size: 10px;
    color: var(--text-dim);
    letter-spacing: 0.2px;
}

/* ---- toggle button (always visible; top-right, next to the menu) ---- */
#panel-toggle {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 12;
    pointer-events: auto;

    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;

    color: var(--text);
    background: var(--bg-panel);
    -webkit-backdrop-filter: blur(14px);
    backdrop-filter: blur(14px);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform 0.15s ease, background 0.15s ease;
}

#panel-toggle:hover { background: rgba(40, 44, 54, 0.8); }
#panel-toggle:active { transform: scale(0.94); }

/* ---- the control panel (right sidebar on desktop) ---- */
#panel {
    position: fixed;
    top: 72px;
    right: 16px;
    z-index: 11;
    pointer-events: auto;

    width: 336px;
    max-height: calc(100vh - 88px);
    padding: 18px 18px 22px;

    color: var(--text);
    background: var(--bg-panel);
    -webkit-backdrop-filter: blur(18px) saturate(140%);
    backdrop-filter: blur(18px) saturate(140%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);

    display: flex;
    flex-direction: column;
    overflow: hidden auto;
    touch-action: pan-y;

    transition: transform 0.32s cubic-bezier(0.22, 1, 0.36, 1),
                opacity 0.32s ease;
}

#panel-header {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 14px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

#panel-heading {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

/* closes the panel: the sheet on mobile, the sidebar on desktop */
#panel-close {
    flex: 0 0 auto;
    width: 30px;
    height: 30px;
    margin: -4px -4px 0 0;
    display: flex;
    align-items: center;
    justify-content: center;

    color: var(--text-dim);
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    pointer-events: auto;
    -webkit-appearance: none;
    appearance: none;
    transition: background 0.15s ease, color 0.15s ease;
}

#panel-close:hover { background: rgba(255, 255, 255, 0.1); color: var(--text); }

#panel-title {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: 0.2px;
}

#panel-subtitle {
    font-size: 11px;
    color: var(--text-dim);
}

#panel-body {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.section {
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.control-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.4px;
    text-transform: uppercase;
    color: var(--text-dim);
}

/* ---- action buttons ---- */
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;

    flex: 1 1 0;
    min-width: 0;
    height: 38px;
    padding: 0;

    font-family: var(--font);
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    pointer-events: auto;
    transition: transform 0.12s ease, background 0.15s ease, opacity 0.15s ease;
    -webkit-user-select: none;
    user-select: none;
    -webkit-appearance: none;
    appearance: none;
}

/* second tap on "清空" asks for confirmation before wiping the painting.
   Specificity is doubled so it also wins over .button-danger. */
.button.button-confirm {
    background: var(--danger);
    border-color: transparent;
    color: #fff;
}

.button:hover { background: rgba(255, 255, 255, 0.16); }
.button:active { transform: scale(0.96); }

.button-primary { background: var(--accent); border-color: transparent; color: #06121f; }
.button-primary:hover { background: #6cb4ff; }

.button-danger { background: rgba(255, 77, 94, 0.16); border-color: rgba(255, 77, 94, 0.4); color: #ffd2d6; }
.button-danger:hover { background: rgba(255, 77, 94, 0.28); }

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

/* ---- segmented controls (Quality / Color Mode) ---- */
.buttons {
    display: flex;
    width: 100%;
    height: 34px;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.2);
}

.buttons button {
    flex: 1 1 0;
    display: flex;
    align-items: center;
    justify-content: center;

    font-family: var(--font);
    font-size: 12px;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
    pointer-events: auto;
    color: var(--text-dim);
    background: transparent;
    border: none;
    border-right: 1px solid var(--border);
    transition: background 0.15s ease, color 0.15s ease;
    -webkit-user-select: none;
    user-select: none;
    -webkit-appearance: none;
    appearance: none;
}

.buttons button:last-child { border-right: none; }

/* keyboard focus ring for every interactive control */
.button:focus-visible,
.buttons button:focus-visible,
#panel-toggle:focus-visible,
#panel-close:focus-visible,
#color-swatch:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* specificity is doubled so these win over the "background: transparent" reset
   that .buttons button applies to every segment */
.buttons button.button-selected {
    background: var(--accent);
    color: #06121f;
    cursor: default;
}

.buttons button.button-unselected:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }

.do-button-active { cursor: pointer; }
.do-button-inactive { opacity: 0.4; cursor: default; }

/* ---- recently used colors ---- */
.swatches {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.swatch {
    width: 30px;
    height: 30px;
    padding: 0;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.55);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
    cursor: pointer;
    pointer-events: auto;
    -webkit-appearance: none;
    appearance: none;
    transition: transform 0.12s ease;
}

.swatch:active { transform: scale(0.9); }
.swatch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- sliders ---- */
.slider {
    position: relative;
    width: 100%;
    height: 22px;
    cursor: ew-resize;
    pointer-events: auto;
    touch-action: none;
}

/* ---- color picker: floating card over the canvas ----
   #color-picker-area is a transparent window: the picker is rendered on the WebGL canvas
   underneath and shows through it, so the card frame belongs on the wrap.
   Deliberately no backdrop-filter - it would blur the picker drawn below.
   It is a popup: hidden by default and revealed either because the desktop panel is open
   or because the user tapped the color swatch (.picker-open). */
/* bottom-left: the right side is taken by the menu panel */
#color-picker-wrap {
    position: fixed;
    left: 16px;
    bottom: 16px;
    z-index: 11;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease;

    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: flex-start;
    padding: 12px;

    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

/* desktop: show the picker whenever the panel (sidebar) is open */
#ui:not(.collapsed) #color-picker-wrap { opacity: 1; }

/* explicit open (mobile swatch tap) wins over the rules above */
#ui.picker-open #color-picker-wrap { opacity: 1; }

#color-picker-area {
    width: 300px;
    height: 200px;
    pointer-events: auto; /* captures events, forwarded to the WebGL picker */
    touch-action: none;
    cursor: crosshair;

    /* no border/background/radius here: the WebGL picker is drawn on the canvas below
       and has to show through, so the card frame lives on #color-picker-wrap */
}

/* ---- scrim: tapping outside the mobile sheet closes it ----
   Hidden on desktop, where the panel is a permanent sidebar. */
#panel-scrim {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10; /* below the panel (11) */
    background: rgba(0, 0, 0, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

/* ---- collapsed state (full panel hidden) ----
   the panel now lives on the right, so it slides out to the right */
#ui.collapsed #panel {
    transform: translateX(calc(100% + 24px));
    opacity: 0;
    pointer-events: none;
}

/* ---- instructions + footer ---- */
/* top-left, directly under the brand (top-right is taken by the menu button) */
#instructions {
    position: fixed;
    left: 18px;
    top: 78px;
    z-index: 10;
    max-width: 240px;
    text-align: left;

    line-height: 1.5;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.7);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
    pointer-events: none;

    transition: opacity 0.35s ease;
}

/* faded out once the user has made their first stroke */
#instructions.hidden {
    opacity: 0;
}

#instructions span {
    font-weight: 700;
    color: #fff;
}

#footer {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 18px;
    z-index: 10;

    font-size: 11px;
    color: rgba(255, 255, 255, 0.55);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
    pointer-events: auto;
}

#footer a {
    color: rgba(255, 255, 255, 0.75);
    text-decoration: none;
}
#footer a:hover { color: #fff; text-decoration: underline; }

/* ---- quick bar (mobile): color swatch + brush size, always visible ---- */
#quickbar {
    display: none; /* overridden in the mobile media query */
}

#color-swatch {
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.85);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
    background: #ffffff;
    cursor: pointer;
    pointer-events: auto;
    transition: transform 0.12s ease;
}

#color-swatch:active { transform: scale(0.92); }

#quick-size {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

#quick-size .control-label { white-space: nowrap; }

#quick-size .slider { flex: 1 1 auto; }

/* ---- unsupported ---- */
#no-support-container {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 18px;
    padding: 24px;
    text-align: center;

    font-size: 16px;
    color: #ddd;
    background: #0c0d11;
}

#no-support-container a { color: var(--accent); }

/* ============ MOBILE ============ */
@media (max-width: 720px) {
    /* always-visible slim quick bar at the bottom */
    #quickbar {
        display: flex;
        align-items: center;
        gap: 12px;
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        height: calc(var(--quickbar-h) + env(safe-area-inset-bottom, 0px));
        padding: 0 14px calc(env(safe-area-inset-bottom, 0px));
        z-index: 13;
        pointer-events: auto;

        color: var(--text);
        background: var(--bg-panel-solid);
        -webkit-backdrop-filter: blur(18px) saturate(140%);
        backdrop-filter: blur(18px) saturate(140%);
        border-top: 1px solid var(--border);
    }

    #panel {
        top: auto;
        left: 50%;
        right: auto; /* desktop anchors it to the right; here it is a centered sheet */
        /* sit above the quick bar */
        bottom: calc(var(--quickbar-h) + 10px);
        transform: translateX(-50%);
        width: min(94vw, 360px);
        min-width: 0;
        max-height: calc(100vh - var(--quickbar-h) - 84px);
        padding: 14px 14px 18px;
    }

    .actions { grid-template-columns: 1fr 1fr; }

    #ui.collapsed #panel {
        transform: translate(-50%, calc(100% + 20px));
    }

    /* dim the canvas behind the sheet; tapping it closes the panel */
    #panel-scrim { display: block; }
    #ui:not(.collapsed) #panel-scrim { opacity: 1; pointer-events: auto; }

    /* the picker is a popup above the quick bar, only shown via .picker-open */
    #color-picker-wrap {
        right: auto;
        left: 50%;
        bottom: calc(var(--quickbar-h) + 12px);
        transform: translateX(-50%);
        align-items: center;
        padding: 10px;
        gap: 5px;
    }

    /* on mobile, opening the full panel must NOT auto-show the picker popup */
    #ui:not(.collapsed) #color-picker-wrap { opacity: 0; }
    #ui.picker-open #color-picker-wrap { opacity: 1; }

    /* shrink the picker frame on phones so it doesn't cover most of the canvas */
    #color-picker-area {
        width: min(280px, 72vw);
        height: 150px;
    }

    /* compact recent-color swatches so they stay on one row under the picker */
    .swatches { gap: 7px; justify-content: center; }
    .swatch { width: 26px; height: 26px; }

    /* compact brand on phones */
    #brand {
        padding: 6px 11px 6px 8px;
        gap: 8px;
    }
    #brand-logo { width: 24px; height: 24px; }
    #brand-title { font-size: 13px; }
    #brand-subtitle { display: none; }

    #instructions {
        top: 74px;
        left: 14px;
        right: auto;
        bottom: auto;
        max-width: 62vw;
        text-align: left;
        font-size: 10px;
    }

    #footer { display: none; }
}

/* very small / landscape phones */
@media (max-width: 720px) and (max-height: 480px) {
    #panel { max-height: calc(100vh - var(--quickbar-h) - 70px); }
    #color-picker-wrap { bottom: calc(var(--quickbar-h) + 10px); }
    #instructions { display: none; }
}
