:root {
    --surface: #ffffff;
    --surface-subtle: #f8f8f8;
    --surface-muted: #f1f1f3;
    --field-bg: #fcfcfd;
    --ink: #18181b;
    --ink-secondary: #52525b;
    --ink-muted: #71717a;
    --brand-muted: #666f7e;
    --border: #e4e4e7;
    --border-strong: #d4d4d8;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
}

html.dark-mode {
    --surface: #141416;
    --surface-subtle: #1c1c1f;
    --surface-muted: #242429;
    --field-bg: #18181c;
    --ink: #f0f0f2;
    --ink-secondary: #a1a1aa;
    --ink-muted: #71717a;
    --brand-muted: #a1a1aa;
    --border: #27272a;
    --border-strong: #3f3f46;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    background-color: var(--surface);
    color: var(--ink);
    font-family: "Avenir Next", "Avenir", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}

html.theme-ready,
html.theme-ready body,
html.theme-ready .site-header,
html.theme-ready .toolbar,
html.theme-ready .field-stage,
html.theme-ready .site-footer {
    transition: background-color 0.25s ease, color 0.25s ease, border-color 0.25s ease;
}

a {
    color: inherit;
    text-decoration: none;
}

button, input {
    font-family: inherit;
}

/* Header */
.site-header {
    background-color: var(--surface-subtle);
    border-bottom: 1px solid var(--border);
    height: 4rem;
    padding: 0 1.5rem;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 40;
}

.back-btn {
    display: inline-flex;
    align-items: center;
    justify-self: start;
    gap: 0.45rem;
    font-family: Arial, sans-serif;
    font-size: 0.8rem;
    letter-spacing: 2px;
    line-height: 1;
    text-transform: uppercase;
    color: var(--brand-muted);
    transition: color 0.2s ease;
}

.back-btn:hover {
    color: var(--ink);
}

.header-brand {
    font-family: Arial, sans-serif;
    font-size: 0.9rem;
    letter-spacing: 5px;
    text-indent: 5px;
    color: var(--brand-muted);
    justify-self: center;
    text-align: center;
    line-height: 1;
    white-space: nowrap;
}

/* Theme Toggle */
.theme-toggle {
    position: relative;
    justify-self: end;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    margin-right: -0.375rem;
    padding: 0;
    border: none;
    border-radius: 9999px;
    background: transparent;
    color: var(--brand-muted);
    cursor: pointer;
    z-index: 15;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.theme-toggle:hover {
    background-color: rgba(0, 0, 0, 0.06);
    color: var(--ink);
}

html.dark-mode .theme-toggle:hover {
    background-color: rgba(255, 255, 255, 0.08);
}

.theme-toggle:focus-visible {
    outline: 2px solid var(--brand-muted);
    outline-offset: 2px;
}

.theme-toggle svg {
    width: 1.75rem;
    height: 1.75rem;
}

.theme-toggle .icon-sun {
    display: block;
}

.theme-toggle .icon-moon {
    display: none;
}

html.dark-mode .theme-toggle .icon-sun {
    display: none;
}

html.dark-mode .theme-toggle .icon-moon {
    display: block;
}

/* Workspace */
.workspace {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    padding: 1.25rem 1.5rem 1.5rem;
    gap: 0.85rem;
}

/* Toolbar */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.85rem 1.25rem;
    background-color: var(--surface-subtle);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 0.65rem 1rem;
}

.tool-group {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.tool-label {
    font-family: Arial, sans-serif;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--ink-secondary);
    user-select: none;
}

.tool-divider {
    width: 1px;
    height: 1.5rem;
    background-color: var(--border);
}

/* Color Swatches & Picker */
.swatches {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    flex-wrap: wrap;
}

.swatch {
    width: 1.55rem;
    height: 1.55rem;
    border-radius: 9999px;
    border: 2px solid transparent;
    background-color: var(--swatch-color);
    cursor: pointer;
    padding: 0;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.swatch:hover {
    transform: scale(1.1);
}

.swatch.active {
    box-shadow: 0 0 0 2px var(--surface-subtle), 0 0 0 4px var(--ink);
    transform: scale(1.08);
}

.custom-color-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.7rem;
    height: 1.7rem;
    border-radius: 9999px;
    cursor: pointer;
    background: conic-gradient(
        #ef4444,
        #f59e0b,
        #10b981,
        #3b82f6,
        #8b5cf6,
        #ec4899,
        #ef4444
    );
    padding: 2px;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.custom-color-wrap:hover {
    transform: scale(1.1);
}

.custom-color-wrap.active {
    box-shadow: 0 0 0 2px var(--surface-subtle), 0 0 0 4px var(--ink);
}

.custom-color-wrap input[type="color"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    border: none;
}

.custom-color-Preview {
    width: 100%;
    height: 100%;
    border-radius: 9999px;
    border: 2px solid var(--surface-subtle);
    background-color: transparent;
    pointer-events: none;
}

/* Stroke Size Pills */
.size-pills {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    background-color: var(--surface-muted);
    padding: 3px;
    border-radius: var(--radius-sm);
}

.size-pill {
    width: 2rem;
    height: 1.75rem;
    border: none;
    border-radius: 4px;
    background: transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--ink-secondary);
    transition: background-color 0.15s ease, color 0.15s ease;
}

.size-pill .dot {
    border-radius: 9999px;
    background-color: currentColor;
    display: block;
}

.size-pill:hover {
    color: var(--ink);
}

.size-pill.active {
    background-color: var(--surface);
    color: var(--ink);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Action Buttons */
.actions-group {
    margin-left: auto;
    gap: 0.5rem;
}

.action-btn {
    border: 1px solid var(--border-strong);
    background-color: var(--surface);
    color: var(--ink);
    padding: 0.42rem 0.85rem;
    font-family: Arial, sans-serif;
    font-size: 0.74rem;
    font-weight: 600;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
    white-space: nowrap;
}

.action-btn:hover {
    background-color: var(--surface-muted);
    border-color: var(--ink-muted);
}

.action-btn.danger-btn:hover {
    border-color: #dc2626;
    color: #dc2626;
    background-color: rgba(220, 38, 38, 0.06);
}

html.dark-mode .action-btn.danger-btn:hover {
    border-color: #f87171;
    color: #f87171;
    background-color: rgba(248, 113, 113, 0.1);
}

/* Open Physics Field */
.field-stage {
    position: relative;
    flex: 1;
    min-height: clamp(460px, calc(100dvh - 13.5rem), 920px);
    width: 100%;
    background-color: var(--field-bg);
    background-image: radial-gradient(var(--border-strong) 1px, transparent 1px);
    background-size: 24px 24px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.03);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

#physicsCanvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
    touch-action: none;
}

#physicsCanvas.grabbing-cursor {
    cursor: grabbing;
}

#physicsCanvas.hover-grab-cursor {
    cursor: grab;
}

.field-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 1.5rem;
    pointer-events: none;
}

html.theme-ready .field-overlay {
    transition: opacity 0.35s ease;
}

.field-overlay.hidden {
    opacity: 0;
}

.overlay-title {
    font-family: Arial, sans-serif;
    font-size: clamp(1rem, 2vw, 1.35rem);
    font-weight: 400;
    letter-spacing: 3px;
    text-transform: uppercase;
    color: var(--ink-secondary);
    opacity: 0.65;
    margin-bottom: 0.45rem;
}

.overlay-sub {
    font-size: clamp(0.8rem, 1.3vw, 0.92rem);
    color: var(--ink-muted);
    letter-spacing: 1px;
    opacity: 0.7;
}

.field-status {
    position: absolute;
    bottom: 0.75rem;
    right: 0.9rem;
    pointer-events: none;
    font-family: Arial, sans-serif;
    font-size: 0.7rem;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    color: var(--ink-muted);
    background-color: var(--surface-subtle);
    border: 1px solid var(--border);
    padding: 0.25rem 0.6rem;
    border-radius: 9999px;
    opacity: 0.85;
}

/* Footer */
.site-footer {
    text-align: center;
    padding: 1rem 1.25rem;
    background-color: var(--surface-subtle);
    border-top: 1px solid var(--border);
}

.copyright {
    font-family: Arial, sans-serif;
    font-size: 0.75rem;
    letter-spacing: 4px;
    color: var(--brand-muted);
}

/* Responsive */
@media (max-width: 768px) {
    .site-header {
        padding: max(env(safe-area-inset-top, 0px), 0.75rem) 1rem 0 1rem;
        height: calc(3.5rem + max(env(safe-area-inset-top, 0px), 0.75rem));
    }

    .workspace {
        padding: 1rem 1rem 1.25rem;
        gap: 0.65rem;
    }

    .toolbar {
        padding: 0.55rem 0.75rem;
        gap: 0.6rem 0.75rem;
    }

    .tool-divider {
        display: none;
    }

    .actions-group {
        width: 100%;
        margin-left: 0;
        justify-content: space-between;
    }

    .action-btn {
        flex: 1;
        text-align: center;
        padding: 0.45rem 0.5rem;
        font-size: 0.68rem;
    }

    .field-stage {
        min-height: clamp(400px, calc(100dvh - 15.5rem), 760px);
    }
}

@media (max-width: 580px) {
    .back-btn span {
        display: none;
    }

    .back-btn svg {
        width: 18px;
        height: 18px;
    }

    .header-brand {
        font-size: 0.75rem;
        letter-spacing: 3px;
        text-indent: 3px;
    }

    .tool-label {
        display: none;
    }

    .swatch {
        width: 1.4rem;
        height: 1.4rem;
    }
}
