/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0f1117;
    --bg-panel: #161822;
    --bg-surface: #1c1e2e;
    --bg-hover: #252840;
    --border: #2a2d45;
    --text: #e1e4f0;
    --text-dim: #8b8fa8;
    --text-bright: #ffffff;
    --accent: #6c5ce7;
    --accent-glow: rgba(108, 92, 231, 0.3);
    --accent2: #00cec9;
    --error: #ff6b6b;
    --error-bg: rgba(255, 107, 107, 0.1);
    --success: #00b894;
    --shadow: 0 8px 32px rgba(0,0,0,0.4);
    --radius: 12px;
    --radius-sm: 8px;
    --radius-xs: 4px;
    --font: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
    --header-h: 56px;
    --footer-h: 36px;
    --panel-header-h: 40px;
}

/* ===== Layout ===== */
html, body {
    height: 100%;
    overflow: hidden;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-height: 100vh;
}

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-h);
    padding: 0 16px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    gap: 12px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.logo {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-bright);
    letter-spacing: -0.3px;
}

.badge {
    font-size: 10px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 20px;
    background: linear-gradient(135deg, var(--accent), var(--accent2));
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.header-center {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: visible;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 16px;
}

.ctrl-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.ctrl-label {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Select Dropdown */
#templateSelect,
#toolSelect {
    padding: 4px 28px 4px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    background: var(--bg-surface);
    color: var(--text);
    font-size: 12px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    outline: none;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238b8fa8'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    transition: border-color 0.2s;
    min-width: 120px;
}

#templateSelect:hover,
#toolSelect:hover {
    border-color: var(--accent);
}

#templateSelect:focus,
#toolSelect:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Theme Swatches */
.theme-swatches {
    display: flex;
    gap: 4px;
}

.swatch {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid var(--border);
    cursor: pointer;
    transition: all 0.2s;
    padding: 0;
    position: relative;
}

.swatch:hover {
    transform: scale(1.2);
}

.swatch.active {
    border-color: var(--accent);
    box-shadow: 0 0 8px var(--accent-glow);
}

.swatch.active::after {
    content: '✓';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}

.header-right {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border: none;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--text-dim);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.icon-btn:hover {
    background: var(--bg-hover);
    color: var(--text-bright);
}

/* ===== Main ===== */
.main {
    display: flex;
    flex: 1;
    overflow: hidden;
    gap: 0;
}

/* ===== Panels ===== */
.panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.editor-panel {
    border-right: 1px solid var(--border);
    flex: 0 0 28%;
    max-width: 35%;
}

.preview-panel {
    flex: 1;
    min-width: 0;
}

.ai-panel {
    flex: 0 0 460px;
    max-width: 500px;
    border-left: 1px solid var(--border);
    background: var(--bg-panel);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--panel-header-h);
    padding: 0 16px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.panel-header h2 {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-dim);
}

.editor-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.line-count {
    font-size: 11px;
    color: var(--text-dim);
    font-family: var(--font-mono);
}

/* ===== Buttons ===== */
.btn {
    padding: 6px 16px;
    border: none;
    border-radius: var(--radius-xs);
    background: var(--accent);
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn:hover {
    box-shadow: 0 0 20px var(--accent-glow);
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
}

.btn-sm {
    padding: 4px 10px;
    font-size: 11px;
}

.btn-secondary {
    background: var(--bg-hover);
    color: var(--text);
}

.btn-secondary:hover {
    background: var(--border);
    box-shadow: none;
}

/* ===== Editor ===== */
.editor-wrap {
    flex: 1;
    position: relative;
    overflow: hidden;
}

#editor {
    width: 100%;
    height: 100%;
    padding: 16px;
    border: none;
    outline: none;
    resize: none;
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.6;
    background: var(--bg);
    color: var(--text);
    tab-size: 2;
}

#editor::placeholder {
    color: var(--text-dim);
    opacity: 0.5;
}

.editor-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    pointer-events: none;
}

.error-box {
    pointer-events: auto;
    padding: 12px 16px;
    background: var(--error-bg);
    border-top: 2px solid var(--error);
    backdrop-filter: blur(8px);
}

.error-title {
    font-size: 11px;
    font-weight: 700;
    color: var(--error);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.error-msg {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--error);
    line-height: 1.5;
    max-height: 80px;
    overflow-y: auto;
}

.editor-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 32px;
    padding: 0 12px;
    background: var(--bg-surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.shortcuts {
    font-size: 11px;
    color: var(--text-dim);
}

.shortcuts kbd {
    padding: 1px 5px;
    border-radius: 3px;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: 10px;
    margin: 0 2px;
}

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: var(--text-dim);
    cursor: pointer;
    user-select: none;
}

.toggle input { display: none; }

.toggle-slider {
    width: 32px;
    height: 18px;
    border-radius: 10px;
    background: var(--border);
    position: relative;
    transition: background 0.2s;
}

.toggle-slider::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--text-dim);
    transition: all 0.2s;
}

.toggle input:checked + .toggle-slider {
    background: var(--accent);
}

.toggle input:checked + .toggle-slider::after {
    left: 16px;
    background: #fff;
}

/* ===== AI Chat Panel ===== */
.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-msg {
    display: flex;
    gap: 8px;
    max-width: 100%;
}

.ai-msg-system {
    align-self: flex-start;
}

.ai-msg-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-msg-assistant {
    align-self: flex-start;
}

.ai-msg-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--bg-surface);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
}

.ai-msg-content {
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    line-height: 1.5;
    max-width: calc(100% - 40px);
    word-break: break-word;
}

.ai-msg-system .ai-msg-content {
    background: var(--bg-surface);
    color: var(--text-dim);
}

.ai-msg-user .ai-msg-content {
    background: var(--accent);
    color: #fff;
}

.ai-msg-assistant .ai-msg-content {
    background: var(--bg-surface);
    color: var(--text);
}

.ai-msg-content code {
    display: block;
    padding: 6px 8px;
    margin: 6px 0;
    background: var(--bg);
    border-radius: var(--radius-xs);
    font-family: var(--font-mono);
    font-size: 11px;
    line-height: 1.4;
    white-space: pre-wrap;
    word-break: break-all;
    color: var(--accent2);
}

.ai-msg-content .apply-btn {
    margin-top: 6px;
    padding: 3px 10px;
    border: 1px solid var(--accent);
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--accent);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.ai-msg-content .apply-btn:hover {
    background: var(--accent);
    color: #fff;
}

.ai-msg-loading .ai-msg-content::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-left: 4px;
    border-radius: 50%;
    background: var(--accent);
    animation: aiDot 1s ease-in-out infinite;
}

@keyframes aiDot {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

.ai-input-wrap {
    display: flex;
    gap: 6px;
    padding: 8px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

#aiInput {
    flex: 1;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 12px;
    line-height: 1.4;
    resize: none;
    outline: none;
    transition: border-color 0.2s;
}

#aiInput:focus {
    border-color: var(--accent);
}

#aiInput::placeholder {
    color: var(--text-dim);
    opacity: 0.6;
}

.btn-ai {
    padding: 12px 16px;
    align-self: stretch;
    white-space: nowrap;
}

/* ===== Footer ===== */
.footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--footer-h);
    padding: 0 16px;
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.footer span {
    font-size: 11px;
    color: var(--text-dim);
}

.footer a {
    color: var(--accent2);
    text-decoration: none;
}

.footer a:hover {
    text-decoration: underline;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-dim);
}

/* ===== Responsive ===== */
@media (max-width: 1100px) {
    .ai-panel {
        flex: 0 0 400px;
        max-width: 420px;
    }
}

@media (max-width: 900px) {
    .main {
        flex-direction: column;
    }
    .editor-panel {
        max-width: 100%;
        flex: 0 0 100%;
        max-height: 35vh;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .preview-panel {
        min-height: 300px;
    }
    .ai-panel {
        flex: 0 0 auto;
        max-width: 100%;
        max-height: 280px;
        border-left: none;
        border-top: 1px solid var(--border);
    }
    .ai-messages {
        max-height: 140px;
    }
}

@media (max-width: 700px) {
    .header-center {
        display: none;
    }
    .header {
        flex-wrap: wrap;
        height: auto;
        padding: 8px 12px;
        gap: 8px;
    }
}

/* ===== Printing ===== */
@media print {
    .header, .editor-panel, .editor-footer, .footer { display: none; }
    .preview-panel { border: none; }
    .preview-wrap { padding: 0; }
}

/* ===== Fullscreen ===== */
.preview-panel:fullscreen {
    background: var(--bg);
    padding: 24px;
    display: flex;
    flex-direction: column;
}

.preview-panel:fullscreen .panel-header {
    flex-shrink: 0;
}

.preview-panel:fullscreen .preview-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-panel:-webkit-full-screen {
    background: var(--bg);
    padding: 24px;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.preview-panel:-webkit-full-screen .preview-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ===== Theme Overrides ===== */
.light-theme {
    --bg: #f5f6fa;
    --bg-panel: #ffffff;
    --bg-surface: #f0f1f5;
    --bg-hover: #e4e6ed;
    --border: #d5d7e0;
    --text: #2d2f3a;
    --text-dim: #7a7d8f;
    --text-bright: #1a1b26;
    --shadow: 0 8px 32px rgba(0,0,0,0.1);
}

.theme-forest {
    --bg: #1a2e1a;
    --bg-panel: #142814;
    --bg-surface: #1b3a1b;
    --bg-hover: #2e5a2e;
    --border: #2e5a2e;
    --text: #e8f5e9;
    --text-dim: #81c784;
    --text-bright: #ffffff;
}

.theme-ocean {
    --bg: #0a1628;
    --bg-panel: #0a1628;
    --bg-surface: #0d2137;
    --bg-hover: #1a3a5c;
    --border: #1a3a5c;
    --text: #e3f2fd;
    --text-dim: #64b5f6;
    --text-bright: #ffffff;
}

.theme-sunset {
    --bg: #2d1b1b;
    --bg-panel: #2d1b1b;
    --bg-surface: #3d1e1e;
    --bg-hover: #5a2a2a;
    --border: #5a2a2a;
    --text: #fbe9e7;
    --text-dim: #ffab91;
    --text-bright: #ffffff;
}

.theme-neutral {
    --bg: #1a1a1a;
    --bg-panel: #1a1a1a;
    --bg-surface: #2a2a2a;
    --bg-hover: #3a3a3a;
    --border: #3a3a3a;
    --text: #f5f5f5;
    --text-dim: #bdbdbd;
    --text-bright: #ffffff;
}
