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

:root {
    --bg: #1a1410;
    --surface: #2a2218;
    --surface-2: #3d3226;
    --border: rgba(212, 165, 116, 0.15);
    --text: #f5e6d3;
    --muted: rgba(245, 230, 211, 0.55);
    --gold: #d4a574;
    --gold-bright: #e8c49a;
    --cream: #f5e6d3;
    --danger: #c0392b;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

.app {
    height: 100%;
    display: flex;
    flex-direction: column;
    max-width: 1200px;
    margin: 0 auto;
    background: var(--surface);
    box-shadow: 0 0 80px rgba(0, 0, 0, 0.45);
}

.header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background:
        radial-gradient(circle at 20% 0%, rgba(212, 165, 116, 0.12), transparent 50%),
        var(--surface);
}

.header-back {
    color: var(--gold);
    text-decoration: none;
    font-size: 1.4rem;
    line-height: 1;
    padding: 8px;
    border-radius: 10px;
}

.header-back:hover {
    background: rgba(212, 165, 116, 0.12);
}

.header-info {
    flex: 1;
}

.header-title {
    font-size: 1.35rem;
    font-weight: 800;
    letter-spacing: 0.04em;
    background: linear-gradient(135deg, var(--gold-bright), var(--gold));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header-tagline {
    font-size: 0.75rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.12em;
}

.tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.tab {
    flex: 1;
    padding: 12px;
    border: none;
    background: transparent;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.tab.active {
    color: var(--gold-bright);
    background: rgba(212, 165, 116, 0.08);
    box-shadow: inset 0 -2px 0 var(--gold);
}

.main {
    flex: 1;
    overflow: hidden;
    min-height: 0;
}

.panel {
    display: none;
    height: 100%;
    overflow: auto;
}

.panel.active {
    display: block;
}

.studio-layout {
    display: flex;
    height: 100%;
    min-height: 0;
}

.toolbar {
    width: 200px;
    flex-shrink: 0;
    padding: 16px 12px;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    background: var(--surface-2);
}

.toolbar-label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--muted);
    margin-bottom: 8px;
    margin-top: 12px;
}

.toolbar-label:first-child {
    margin-top: 0;
}

.tool-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 4px;
    border: 2px solid transparent;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.2);
    color: var(--text);
    cursor: pointer;
    font-size: 0.65rem;
}

.tool-btn.active {
    border-color: var(--gold);
    background: rgba(212, 165, 116, 0.15);
}

.tool-icon {
    font-size: 1.1rem;
}

.size-slider {
    width: 100%;
    accent-color: var(--gold);
}

.color-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 4px;
}

.color-btn {
    aspect-ratio: 1;
    border: 2px solid transparent;
    border-radius: 6px;
    cursor: pointer;
}

.color-btn.active {
    border-color: var(--gold-bright);
    box-shadow: 0 0 0 2px rgba(212, 165, 116, 0.4);
}

.color-custom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--muted);
}

.color-custom input[type="color"] {
    width: 36px;
    height: 28px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    background: transparent;
}

.toolbar-actions {
    display: flex;
    gap: 6px;
    margin-top: 16px;
}

.canvas-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 16px;
    min-width: 0;
    background:
        radial-gradient(circle at 50% 50%, rgba(212, 165, 116, 0.06), transparent 70%),
        var(--bg);
}

#artCanvas {
    max-width: 100%;
    max-height: calc(100vh - 200px);
    width: auto;
    height: auto;
    border-radius: 4px;
    box-shadow:
        0 4px 24px rgba(0, 0, 0, 0.4),
        0 0 0 1px rgba(212, 165, 116, 0.2);
    cursor: crosshair;
    touch-action: none;
}

.canvas-hint {
    margin-top: 10px;
    font-size: 0.72rem;
    color: var(--muted);
}

.btn {
    border: none;
    border-radius: 10px;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.12s, box-shadow 0.12s;
}

.btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.btn-tool {
    flex: 1;
    padding: 10px;
    background: rgba(0, 0, 0, 0.25);
    color: var(--text);
    font-size: 1.1rem;
}

.btn-tool:hover:not(:disabled) {
    background: rgba(212, 165, 116, 0.15);
}

.btn-primary {
    width: 100%;
    margin-top: 16px;
    padding: 12px;
    background: linear-gradient(135deg, var(--gold-bright), var(--gold));
    color: #2a1810;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.btn-primary:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 20px rgba(212, 165, 116, 0.35);
}

.btn-gold {
    padding: 8px 14px;
    background: linear-gradient(135deg, var(--gold-bright), #b8894a);
    color: #2a1810;
    font-size: 0.8rem;
}

.btn-gold:hover {
    box-shadow: 0 0 16px rgba(212, 165, 116, 0.4);
}

.btn-ghost-header {
    padding: 8px 14px;
    background: rgba(245, 230, 211, 0.08);
    color: var(--cream);
    font-size: 0.8rem;
    border: 1px solid var(--border);
}

.btn-ghost-header:hover {
    background: rgba(212, 165, 116, 0.15);
}

.btn-ghost {
    padding: 10px 18px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
}

.btn-small {
    padding: 6px 10px;
    font-size: 0.72rem;
    background: rgba(0, 0, 0, 0.3);
    color: var(--text);
}

.btn-danger {
    background: rgba(192, 57, 43, 0.35);
    color: #ffb4aa;
}

.gallery-header {
    padding: 16px 20px 0;
}

.gallery-count {
    font-size: 0.85rem;
    color: var(--muted);
}

.gallery-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
    padding: 16px 20px 32px;
}

.gallery-empty {
    text-align: center;
    padding: 48px 24px;
    color: var(--muted);
}

.gallery-empty.hidden {
    display: none;
}

.gallery-card {
    background: var(--surface-2);
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.gallery-thumb {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    display: block;
    background: var(--cream);
}

.gallery-meta {
    padding: 8px 10px 4px;
}

.gallery-name {
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
}

.gallery-actions {
    display: flex;
    gap: 4px;
    padding: 8px 10px 10px;
}

/* Silk AI */
.ai-layout {
    display: flex;
    height: 100%;
    min-height: 0;
}

.ai-controls {
    width: 280px;
    flex-shrink: 0;
    padding: 20px 16px;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    background: var(--surface-2);
}

.ai-badge {
    display: inline-block;
    padding: 6px 12px;
    border-radius: 999px;
    background: linear-gradient(135deg, rgba(232, 196, 154, 0.25), rgba(184, 137, 74, 0.2));
    border: 1px solid rgba(212, 165, 116, 0.35);
    color: var(--gold-bright);
    font-size: 0.82rem;
    font-weight: 700;
    margin-bottom: 10px;
}

.ai-desc {
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 16px;
    line-height: 1.45;
}

.ai-prompt {
    width: 100%;
    padding: 12px 14px;
    border-radius: 10px;
    border: 2px solid rgba(212, 165, 116, 0.25);
    background: rgba(0, 0, 0, 0.3);
    color: var(--text);
    font-size: 0.95rem;
    font-family: inherit;
    resize: vertical;
    min-height: 72px;
    margin-bottom: 4px;
}

.ai-prompt:focus {
    outline: none;
    border-color: var(--gold);
}

.ai-style-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 4px;
}

.ai-style-btn {
    padding: 6px 10px;
    border-radius: 8px;
    border: 2px solid transparent;
    background: rgba(0, 0, 0, 0.25);
    color: var(--text);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}

.ai-style-btn.active {
    border-color: var(--gold);
    background: rgba(212, 165, 116, 0.15);
    color: var(--gold-bright);
}

.ai-mode-toggle {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
}

.ai-mode {
    flex: 1;
    padding: 8px;
    border-radius: 8px;
    border: 2px solid transparent;
    background: rgba(0, 0, 0, 0.25);
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
}

.ai-mode.active {
    border-color: var(--gold);
    color: var(--gold-bright);
    background: rgba(212, 165, 116, 0.12);
}

.ai-hint {
    font-size: 0.72rem;
    color: var(--muted);
    margin-bottom: 12px;
    line-height: 1.4;
}

.ai-examples {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.ai-example-btn {
    text-align: left;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.2);
    color: var(--muted);
    font-size: 0.72rem;
    cursor: pointer;
    font-family: inherit;
}

.ai-example-btn:hover {
    color: var(--text);
    border-color: rgba(212, 165, 116, 0.35);
}

.ai-status {
    margin-top: 12px;
    font-size: 0.78rem;
    color: var(--gold);
    min-height: 1.2em;
}

.ai-status.error {
    color: #ffb4aa;
}

.ai-preview-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px;
    min-width: 0;
    background:
        radial-gradient(circle at 50% 30%, rgba(212, 165, 116, 0.08), transparent 60%),
        var(--bg);
}

.ai-preview {
    max-width: 100%;
    max-height: calc(100vh - 280px);
    border-radius: 6px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
    border: 1px solid rgba(212, 165, 116, 0.25);
}

.ai-preview.hidden,
.ai-preview-empty.hidden,
.ai-loading.hidden,
.ai-actions.hidden {
    display: none;
}

.ai-preview-empty {
    color: var(--muted);
    font-size: 0.9rem;
    text-align: center;
}

.ai-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    color: var(--muted);
    font-size: 0.9rem;
}

.ai-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(212, 165, 116, 0.2);
    border-top-color: var(--gold);
    border-radius: 50%;
    animation: ai-spin 0.8s linear infinite;
}

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

.ai-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
    flex-wrap: wrap;
    justify-content: center;
}

.ai-actions .btn-primary {
    width: auto;
    margin-top: 0;
    padding: 10px 18px;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 100;
}

.modal.hidden {
    display: none;
}

.modal-panel {
    width: 100%;
    max-width: 360px;
    background: var(--surface-2);
    border-radius: 16px;
    padding: 24px;
    border: 1px solid var(--border);
}

.modal-title {
    font-size: 1.2rem;
    margin-bottom: 16px;
    color: var(--gold-bright);
}

.modal-label {
    display: block;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin-bottom: 8px;
}

.modal-input {
    width: 100%;
    padding: 12px 14px;
    border-radius: 10px;
    border: 2px solid rgba(212, 165, 116, 0.25);
    background: rgba(0, 0, 0, 0.3);
    color: var(--text);
    font-size: 1rem;
    margin-bottom: 20px;
}

.modal-input:focus {
    outline: none;
    border-color: var(--gold);
}

.modal-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.modal-actions .btn-primary {
    width: auto;
    margin-top: 0;
    padding: 10px 20px;
}

@media (max-width: 720px) {
    .studio-layout {
        flex-direction: column-reverse;
    }

    .ai-layout {
        flex-direction: column;
    }

    .ai-controls {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: 50vh;
    }

    .ai-preview {
        max-height: calc(100vh - 420px);
    }

    .toolbar {
        width: 100%;
        border-right: none;
        border-top: 1px solid var(--border);
        max-height: 42vh;
        padding-bottom: calc(12px + var(--safe-bottom));
    }

    .tool-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    #artCanvas {
        max-height: calc(100vh - 340px);
    }
}
