/* 手写板 — 下拉工具栏 + 纸面优先 */

:root {
    --ink: #1c1c1e;
    --ink-soft: #636366;
    --paper: #ffffff;
    --desk: #ebebf0;
    --desk-deep: #e5e5ea;
    --chrome: rgba(255, 255, 255, 0.78);
    --line: rgba(60, 60, 67, 0.12);
    --accent: #0071e3;
    --accent-press: #0058b0;
    --danger: #ff3b30;
    --shadow-soft: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
    --shadow-paper: 0 2px 8px rgba(0, 0, 0, 0.06), 0 12px 32px rgba(0, 0, 0, 0.08);
    --shadow-panel: 0 8px 28px rgba(0, 0, 0, 0.14);
    --radius-sm: 10px;
    --radius-md: 14px;
    --radius-lg: 18px;
    --touch: 44px;
    --header-h: 48px;
    --toolbar-h: 56px;
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --checker: linear-gradient(45deg, #d8d8dc 25%, transparent 25%),
               linear-gradient(-45deg, #d8d8dc 25%, transparent 25%),
               linear-gradient(45deg, transparent 75%, #d8d8dc 75%),
               linear-gradient(-45deg, transparent 75%, #d8d8dc 75%);
    --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC",
            "Source Han Sans SC", "WenQuanYi Micro Hei", "Segoe UI", system-ui, sans-serif;
}

*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    width: 100%;
}

body {
    font-family: var(--font);
    color: var(--ink);
    background:
        radial-gradient(1200px 600px at 10% -10%, rgba(0, 113, 227, 0.06), transparent 55%),
        radial-gradient(900px 500px at 100% 0%, rgba(255, 149, 0, 0.05), transparent 50%),
        linear-gradient(180deg, #f5f5f7 0%, var(--desk) 100%);
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

button {
    font: inherit;
    color: inherit;
    appearance: none;
    -webkit-appearance: none;
}

.app-container {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr);
    height: 100vh;
    height: 100dvh;
    width: 100%;
    max-width: 100vw;
    min-width: 0;
    overflow: hidden;
    padding:
        var(--safe-t)
        var(--safe-r)
        var(--safe-b)
        var(--safe-l);
    animation: appIn 0.35s ease-out;
}

.app-container > * {
    min-width: 0;
    max-width: 100%;
}

@keyframes appIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* —— 顶栏 —— */
.title-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: var(--header-h);
    padding: 6px 14px;
    gap: 12px;
    width: 100%;
}

.title-group {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.title-mark {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent), #64d2ff);
    box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.15);
    flex-shrink: 0;
}

.title-text {
    font-size: 17px;
    font-weight: 650;
    letter-spacing: 0.02em;
    color: var(--ink);
    line-height: 1.2;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.icon-btn {
    width: var(--touch);
    height: var(--touch);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 12px;
    background: var(--chrome);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: var(--ink);
    cursor: pointer;
    transition: background 0.15s ease, transform 0.15s ease, color 0.15s ease;
    box-shadow: inset 0 0 0 1px var(--line);
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

.icon-btn:hover {
    background: #fff;
    color: var(--accent);
}

.icon-btn:active {
    transform: scale(0.94);
}

.icon-btn:disabled {
    opacity: 0.35;
    pointer-events: none;
}

.clear-btn:hover {
    color: var(--danger);
}

.desktop-only {
    display: none;
}

/* —— 紧凑下拉工具栏 —— */
.toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: var(--toolbar-h);
    width: calc(100% - 24px);
    max-width: calc(100% - 24px);
    margin: 0 12px;
    padding: 8px 10px;
    background: var(--chrome);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-soft);
    position: relative;
    z-index: 20;
    overflow: visible;
}

.toolbar-spacer {
    flex: 1;
    min-width: 4px;
}

/* 压感开关 */
.toggle-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 0 10px 0 8px;
    border: none;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.7);
    box-shadow: inset 0 0 0 1px var(--line);
    cursor: pointer;
    flex: 0 0 auto;
    transition: background 0.15s ease, box-shadow 0.15s ease;
}

.toggle-chip:hover {
    background: #fff;
}

.toggle-chip[aria-pressed="true"] {
    box-shadow: inset 0 0 0 1.5px rgba(0, 113, 227, 0.45);
    background: rgba(0, 113, 227, 0.08);
}

.toggle-track {
    width: 34px;
    height: 20px;
    border-radius: 999px;
    background: #d1d1d6;
    position: relative;
    transition: background 0.18s ease;
    flex-shrink: 0;
}

.toggle-chip[aria-pressed="true"] .toggle-track {
    background: var(--accent);
}

.toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    transition: transform 0.18s ease;
}

.toggle-chip[aria-pressed="true"] .toggle-thumb {
    transform: translateX(14px);
}

.toggle-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink-soft);
}

.toggle-chip[aria-pressed="true"] .toggle-label {
    color: var(--accent);
}

.tool-dropdown {
    position: relative;
    flex: 0 0 auto;
}

.dropdown-trigger {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 0 10px 0 12px;
    border: none;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.7);
    box-shadow: inset 0 0 0 1px var(--line);
    cursor: pointer;
    transition: background 0.15s ease, box-shadow 0.15s ease;
}

.dropdown-trigger:hover,
.tool-dropdown.is-open .dropdown-trigger {
    background: #fff;
    box-shadow: inset 0 0 0 1.5px rgba(0, 113, 227, 0.45);
}

.trigger-text {
    font-size: 13px;
    font-weight: 600;
    color: var(--ink-soft);
}

.trigger-swatch {
    --swatch: #fff;
    width: 18px;
    height: 18px;
    background: var(--swatch);
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
    flex-shrink: 0;
}

.trigger-swatch.is-round {
    border-radius: 50%;
}

.trigger-swatch.is-square {
    border-radius: 5px;
}

.trigger-swatch.is-transparent {
    background-color: #fff;
    background-image: var(--checker);
    background-size: 8px 8px;
    background-position: 0 0, 0 4px, 4px -4px, -4px 0;
}

.trigger-thickness {
    width: 22px;
    height: 22px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.trigger-chevron {
    width: 12px;
    height: 12px;
    color: var(--ink-soft);
    transition: transform 0.18s ease;
}

.tool-dropdown.is-open .trigger-chevron {
    transform: rotate(180deg);
}

.dropdown-panel {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    min-width: 220px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: var(--shadow-panel);
    z-index: 30;
    animation: panelIn 0.16s ease-out;
}

.dropdown-panel[hidden] {
    display: none !important;
}

@keyframes panelIn {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

.panel-title {
    font-size: 12px;
    font-weight: 650;
    color: var(--ink);
    margin-bottom: 8px;
}

.panel-hint {
    font-size: 11px;
    color: var(--ink-soft);
    margin: -4px 0 10px;
    line-height: 1.35;
}

.color-palette,
.bg-color-palette,
.thickness-palette {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.color-btn {
    --swatch: #000;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: var(--swatch);
    cursor: pointer;
    position: relative;
    box-shadow:
        inset 0 0 0 1px rgba(0, 0, 0, 0.08),
        0 1px 2px rgba(0, 0, 0, 0.08);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.color-btn[data-color="#FFFFFF"],
.color-btn[data-color="#FFF8E7"] {
    box-shadow:
        inset 0 0 0 1px rgba(0, 0, 0, 0.14),
        0 1px 2px rgba(0, 0, 0, 0.06);
}

.color-btn:hover {
    transform: scale(1.08);
}

.color-btn.active {
    transform: scale(1.08);
    box-shadow:
        0 0 0 2px #fff,
        0 0 0 4px var(--accent),
        inset 0 0 0 1px rgba(0, 0, 0, 0.06);
}

.bg-color-palette .color-btn {
    border-radius: 8px;
    width: 30px;
    height: 30px;
}

.color-btn.is-transparent {
    background-color: #fff;
    background-image: var(--checker);
    background-size: 10px 10px;
    background-position: 0 0, 0 5px, 5px -5px, -5px 0;
}

.thickness-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 10px;
    background: transparent;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, box-shadow 0.15s ease;
}

.thickness-btn:hover {
    background: rgba(0, 113, 227, 0.08);
}

.thickness-btn.active {
    background: rgba(0, 113, 227, 0.12);
    box-shadow: inset 0 0 0 1.5px var(--accent);
}

.thickness-dot {
    width: var(--size);
    height: var(--size);
    border-radius: 50%;
    background: var(--ink);
    display: block;
    pointer-events: none;
}

.action-btn {
    flex: 0 0 auto;
    min-height: 40px;
    min-width: 76px;
    border: none;
    border-radius: 12px;
    background: var(--accent);
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    padding: 0 16px;
    box-shadow: 0 4px 14px rgba(0, 113, 227, 0.28);
    transition: background 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

.action-btn:hover {
    background: var(--accent-press);
    transform: translateY(-1px);
}

.action-btn:active {
    transform: translateY(0) scale(0.98);
}

/* —— 画布 —— */
.canvas-container {
    min-height: 0;
    display: flex;
    padding: 10px 14px calc(12px + var(--safe-b));
}

.canvas-frame {
    position: relative;
    flex: 1;
    min-height: 0;
    min-width: 0;
    border-radius: var(--radius-lg);
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.5), transparent 40%),
        var(--desk-deep);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
    overflow: hidden;
}

.canvas-frame.is-transparent {
    background-color: #efeff4;
    background-image: var(--checker);
    background-size: 16px 16px;
    background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}

#drawingCanvas {
    display: block;
    width: 100%;
    height: 100%;
    background: transparent;
    border-radius: inherit;
    box-shadow: var(--shadow-paper);
    cursor: crosshair;
    touch-action: none;
}

.canvas-frame:not(.is-transparent) #drawingCanvas {
    background: var(--paper);
}

/* —— Toast —— */
.toast-root {
    position: fixed;
    z-index: 10000;
    left: 50%;
    bottom: calc(24px + var(--safe-b));
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
    width: min(92vw, 360px);
}

.toast {
    width: 100%;
    background: rgba(28, 28, 30, 0.92);
    color: #fff;
    padding: 12px 16px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 500;
    text-align: center;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    animation: toastIn 0.28s ease-out;
}

.toast.is-leaving {
    animation: toastOut 0.22s ease-in forwards;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateY(10px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastOut {
    from { opacity: 1; transform: translateY(0); }
    to { opacity: 0; transform: translateY(8px); }
}

/* —— 平板+ —— */
@media (min-width: 768px) {
    .title-bar {
        padding: 8px 20px;
    }

    .toolbar {
        margin: 0 20px;
        width: calc(100% - 40px);
        max-width: calc(100% - 40px);
        padding: 8px 12px;
    }

    .canvas-container {
        padding: 12px 20px 16px;
    }

    .desktop-only {
        display: inline-flex;
    }

    .dropdown-trigger {
        min-height: 42px;
        padding: 0 12px 0 14px;
    }

    .action-btn {
        min-width: 96px;
        min-height: 42px;
    }
}

@media (min-width: 1100px) {
    .app-container {
        max-width: 1600px;
        margin: 0 auto;
    }
}

/* —— 手机 —— */
@media (max-width: 767px) {
    :root {
        --header-h: 46px;
        --toolbar-h: 52px;
        --touch: 40px;
    }

    .title-bar {
        padding: 4px 10px;
    }

    .title-text {
        font-size: 16px;
    }

    .toolbar {
        margin: 0 10px;
        width: calc(100% - 20px);
        max-width: calc(100% - 20px);
        gap: 6px;
        padding: 6px 8px;
        border-radius: 14px;
    }

    .trigger-text {
        font-size: 12px;
    }

    .dropdown-trigger {
        min-height: 38px;
        padding: 0 8px 0 10px;
        gap: 6px;
    }

    .action-btn {
        min-width: 64px;
        min-height: 38px;
        padding: 0 12px;
        font-size: 13px;
    }

    .toggle-chip {
        min-height: 38px;
        padding: 0 8px 0 6px;
        gap: 6px;
    }

    .toggle-label {
        font-size: 12px;
    }

    .canvas-container {
        padding: 8px 10px 10px;
    }

    .canvas-frame,
    #drawingCanvas {
        border-radius: 14px;
    }

    /* 右侧下拉避免溢出屏幕 */
    .tool-dropdown:last-of-type .dropdown-panel,
    .tool-dropdown[data-dropdown="thickness"] .dropdown-panel {
        left: auto;
        right: 0;
    }
}

/* —— 横屏矮屏：工具横条仍比侧栏省事 —— */
@media (max-height: 500px) and (orientation: landscape) {
    .title-bar {
        min-height: 36px;
        padding: 2px 10px;
    }

    .toolbar {
        min-height: 44px;
        margin: 0 8px;
        width: calc(100% - 16px);
        max-width: calc(100% - 16px);
        padding: 4px 8px;
    }

    .dropdown-trigger {
        min-height: 34px;
    }

    .action-btn {
        min-height: 34px;
    }

    .canvas-container {
        padding: 6px 8px 8px;
    }

    .dropdown-panel {
        max-height: min(52vh, 220px);
        overflow-y: auto;
    }
}

@media (hover: none) and (pointer: coarse) {
    .color-btn:hover,
    .thickness-btn:hover,
    .action-btn:hover,
    .icon-btn:hover {
        transform: none;
    }

    .icon-btn:hover {
        background: var(--chrome);
        color: var(--ink);
    }

    .action-btn:hover {
        background: var(--accent);
    }

    .thickness-btn:hover {
        background: transparent;
    }

    .thickness-btn.active:hover {
        background: rgba(0, 113, 227, 0.12);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
}
