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

:root {
    --bg: #fafafa;
    --surface: #ffffff;
    --border: #e8e8e8;
    --text: #171717;
    --text-muted: #737373;
    --accent: #171717;
    --accent-hover: #404040;
    --user-bg: #f4f4f5;
    --assistant-bg: transparent;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

html, body {
    height: 100%;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

.app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* Chat panel */
.chat-panel {
    width: 400px;
    min-width: 320px;
    max-width: 480px;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.chat-header {
    padding: 24px 20px 16px;
    border-bottom: 1px solid var(--border);
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 15px;
    letter-spacing: -0.01em;
}

.logo svg {
    opacity: 0.7;
}

.subtitle {
    margin-top: 6px;
    font-size: 13px;
    color: var(--text-muted);
}

.history-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 12px;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.history-toggle:hover,
.history-toggle[aria-expanded="true"] {
    background: var(--user-bg);
    border-color: #d4d4d4;
}

.history-panel {
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    max-height: 220px;
    overflow-y: auto;
}

.history-panel.hidden {
    display: none;
}

.history-empty {
    padding: 14px 20px;
    font-size: 12px;
    color: var(--text-muted);
}

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

.history-list {
    list-style: none;
}

.history-item {
    border-top: 1px solid var(--border);
}

.history-item button {
    width: 100%;
    padding: 12px 20px;
    border: none;
    background: transparent;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s;
}

.history-item button:hover,
.history-item button.active {
    background: var(--surface);
}

.history-item-title {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    margin-bottom: 2px;
}

.history-item-meta {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
}

.btn-ghost.hidden {
    display: none;
}

.preview-broken-banner {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 999px;
    background: rgba(23, 23, 23, 0.92);
    color: #fff;
    font-size: 12px;
    box-shadow: var(--shadow);
}

.preview-broken-banner.hidden {
    display: none;
}

.preview-broken-banner button {
    border: none;
    border-radius: 999px;
    padding: 4px 10px;
    background: #fff;
    color: var(--text);
    font-family: inherit;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.message {
    display: flex;
    flex-direction: column;
}

.message-user .message-content {
    background: var(--user-bg);
    border-radius: var(--radius);
    padding: 12px 14px;
    align-self: flex-end;
    max-width: 95%;
}

.message-assistant .message-content {
    color: var(--text);
}

.message-content p {
    margin-bottom: 8px;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content ul {
    margin-left: 18px;
    color: var(--text-muted);
    font-size: 13px;
}

.message-content ul li {
    margin-bottom: 4px;
}

.message-content.error {
    color: #dc2626;
    font-size: 13px;
}

.message-loading .message-content {
    display: flex;
    flex-direction: column;
    gap: 10px;
    color: var(--text-muted);
    font-size: 13px;
}

.message-loading-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

.generation-progress-bar {
    width: 100%;
    height: 6px;
    border-radius: 999px;
    background: #e5e5e5;
    overflow: hidden;
}

.generation-progress-fill {
    width: 0%;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #171717, #525252);
    transition: width 0.4s ease;
}

.generation-overlay {
    position: absolute;
    inset: 0;
    z-index: 3;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(4px);
}

.generation-overlay.hidden {
    display: none;
}

.generation-card {
    width: min(320px, 90%);
    text-align: center;
}

.generation-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 16px;
}

.generation-status {
    margin-top: 12px;
    font-size: 13px;
    color: var(--text-muted);
}

.generation-time {
    margin-top: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

.dots {
    display: flex;
    gap: 4px;
}

.dots span {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.dots span:nth-child(1) { animation-delay: -0.32s; }
.dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* Chat form */
.chat-form {
    padding: 16px 20px 20px;
    border-top: 1px solid var(--border);
}

.inspiration-preview {
    position: relative;
    display: inline-block;
    margin-bottom: 10px;
}

.inspiration-preview.hidden {
    display: none;
}

.inspiration-preview img {
    display: block;
    max-width: 120px;
    max-height: 80px;
    border-radius: 8px;
    border: 1px solid var(--border);
    object-fit: cover;
}

.inspiration-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
}

.inspiration-remove:hover {
    background: var(--user-bg);
    color: var(--text);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 10px 10px 14px;
    transition: border-color 0.15s;
}

.input-wrapper:focus-within {
    border-color: #a3a3a3;
}

.input-wrapper textarea {
    flex: 1;
    border: none;
    background: transparent;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 120px;
}

.input-wrapper textarea::placeholder {
    color: var(--text-muted);
}

.attach-btn {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.attach-btn:hover {
    background: var(--user-bg);
    color: var(--text);
}

.input-wrapper #send-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, opacity 0.15s;
}

.input-wrapper #send-btn:hover:not(:disabled) {
    background: var(--accent-hover);
}

.input-wrapper #send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.message-image {
    display: block;
    max-width: 180px;
    max-height: 120px;
    border-radius: 8px;
    margin-bottom: 8px;
    object-fit: cover;
}

.message-image-caption {
    color: var(--text-muted);
    font-size: 13px;
}

/* Preview panel */
.preview-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg);
}

.preview-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.preview-label {
    justify-self: start;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.edit-mode-toggle {
    grid-column: 2;
    justify-self: center;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 500;
    padding: 6px 12px 6px 8px;
    border-radius: 999px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    white-space: nowrap;
}

.edit-mode-toggle:hover {
    background: var(--user-bg);
    color: var(--text);
}

.edit-mode-toggle.is-active {
    background: #eff6ff;
    border-color: #93c5fd;
    color: #1d4ed8;
}

.edit-mode-toggle.hidden {
    display: none;
}

.edit-mode-toggle-track {
    position: relative;
    flex-shrink: 0;
    width: 36px;
    height: 20px;
    border-radius: 999px;
    background: #d4d4d8;
    transition: background 0.2s;
}

.edit-mode-toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
    transition: transform 0.2s;
}

.edit-mode-toggle.is-active .edit-mode-toggle-track {
    background: #3b82f6;
}

.edit-mode-toggle.is-active .edit-mode-toggle-thumb {
    transform: translateX(16px);
}

.edit-mode-toggle-label {
    line-height: 1;
}

.preview-actions {
    grid-column: 3;
    justify-self: end;
    display: flex;
    align-items: center;
    gap: 4px;
}

.btn-publish {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 12px;
    border: none;
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    font: inherit;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s;
}

.btn-publish svg {
    flex-shrink: 0;
}

.btn-publish:hover:not(:disabled) {
    background: var(--accent-hover);
}

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

.btn-ghost {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    transition: background 0.15s, color 0.15s;
}

.btn-ghost:hover:not(:disabled) {
    background: var(--user-bg);
    color: var(--text);
}

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

.preview-container {
    flex: 1;
    position: relative;
    margin: 16px;
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--surface);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}

.preview-empty {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
    font-size: 14px;
    pointer-events: none;
}

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

#preview-frame {
    width: 100%;
    height: 100%;
    border: none;
    display: none;
    background: white;
}

#preview-frame.visible {
    display: block;
}

@media (max-width: 768px) {
    .app {
        flex-direction: column;
    }

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

    .preview-panel {
        height: 50vh;
    }
}

/* Auth & projects pages */
.auth-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: var(--bg);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 32px;
    box-shadow: var(--shadow);
}

.auth-card-wide {
    max-width: 520px;
}

.auth-card-home {
    max-width: 480px;
    text-align: center;
}

.home-brand {
    margin-bottom: 48px;
}

.home-logo {
    display: block;
    width: 200px;
    height: auto;
    margin: 0 auto 16px;
}

.home-title {
    font-size: 28px;
    font-weight: 600;
    letter-spacing: -0.02em;
    color: var(--text);
}

.auth-logo {
    justify-content: center;
    margin-bottom: 16px;
    font-size: 18px;
    font-weight: 600;
}

.auth-lead {
    color: var(--text-muted);
    margin-bottom: 24px;
    line-height: 1.6;
}

.auth-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.auth-card h1,
.projects-header h1 {
    font-size: 24px;
    font-weight: 600;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
}

.auth-subtitle {
    color: var(--text-muted);
    margin-bottom: 24px;
}

.auth-form label {
    display: block;
    margin-bottom: 16px;
}

.auth-form label span {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text);
}

.auth-form input[type="email"],
.auth-form input[type="password"],
.auth-form input[type="text"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
    background: var(--bg);
}

.auth-form input:focus {
    outline: none;
    border-color: #a3a3a3;
}

.auth-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #b91c1c;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 13px;
}

.auth-success {
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    color: #15803d;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 13px;
}

.auth-notice {
    background: #fffbeb;
    border: 1px solid #fde68a;
    color: #92400e;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 20px;
    font-size: 13px;
}

.auth-footer {
    margin-top: 20px;
    text-align: center;
    font-size: 13px;
    color: var(--text-muted);
}

.auth-footer a {
    color: var(--text);
    font-weight: 500;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border-radius: 8px;
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    border: none;
    transition: background 0.15s, color 0.15s;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--user-bg);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--border);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
}

.btn-ghost:hover {
    background: var(--user-bg);
    color: var(--text);
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
}

.form-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 8px;
}

.auth-form .form-hint {
    margin-bottom: 20px;
}

.form-hint a {
    color: var(--text);
}

.projects-page {
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    padding: 40px 24px;
    min-height: 100vh;
    background: var(--bg);
}

.projects-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 32px;
}

.projects-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.projects-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    margin-bottom: 20px;
}

.projects-section h2 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
}

.project-create-form {
    display: flex;
    gap: 10px;
}

.project-create-form input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
    background: var(--bg);
}

.project-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.project-card {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    text-decoration: none;
    color: inherit;
    transition: background 0.15s, border-color 0.15s;
}

.project-card:hover {
    background: var(--user-bg);
    border-color: #d4d4d4;
}

.project-card-name {
    font-weight: 500;
}

.project-card-slug {
    font-size: 12px;
    color: var(--text-muted);
}

.empty-state {
    color: var(--text-muted);
    font-size: 14px;
}

.legacy-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.legacy-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 12px 0;
    border-top: 1px solid var(--border);
}

.legacy-item:first-child {
    border-top: none;
    padding-top: 0;
}

.legacy-meta {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.legacy-import-form {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.legacy-import-form select {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    font-size: 13px;
    background: var(--bg);
}

.app-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    font-size: 12px;
}

.app-back-link {
    color: var(--text-muted);
    text-decoration: none;
}

.app-back-link:hover {
    color: var(--text);
}

.app-topbar-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.app-topbar-link {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 12px;
}

.app-topbar-link:hover {
    color: var(--text);
}

.inline-form {
    display: inline;
}

.app-topbar-button {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    font: inherit;
}

.auth-fieldset {
    border: none;
    padding: 0;
    margin: 0 0 24px;
}

.auth-fieldset legend {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 12px;
    padding: 0;
}

.settings-test {
    margin-top: 20px;
}

.settings-test .form-hint,
.settings-test .auth-success,
.settings-test .auth-error {
    margin-top: 10px;
}

.chat-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}
