* {
    box-sizing: border-box;
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    margin: 0;
    background-color: #f6f7f9;
    color: #1a1a1a;
    line-height: 1.5;
}

.hidden-form {
    display: none;
}

.main-container {
    max-width: 920px;
    margin: 0 auto;
    padding: 2rem 1rem;
}

.page-header {
    text-align: center;
    margin-bottom: 1.5rem;
}

.page-title {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0 0 0.25rem 0;
}

.page-subtitle {
    font-size: 1rem;
    color: #555;
    margin: 0;
}

.form-section {
    background: #ffffff;
    border-radius: 0.5rem;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    margin-bottom: 1.5rem;
}

.tts-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.form-label {
    font-weight: 600;
    font-size: 0.95rem;
}

.form-textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    font-size: 1rem;
    resize: vertical;
    min-height: 120px;
    background-color: #ffffff;
    color: #1a1a1a;
}

.script-textarea {
    font-family: ui-monospace, "Cascadia Code", "Fira Code", monospace;
    font-size: 0.95rem;
    line-height: 1.6;
}

.form-select {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    font-size: 1rem;
    background-color: #ffffff;
    color: #1a1a1a;
}

.voice-select-row {
    display: flex;
    align-items: stretch;
    gap: 0.5rem;
}

.voice-select-row .form-select {
    min-width: 0;
}

.btn-voice-preview {
    flex: 0 0 auto;
    padding: 0.6rem 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    background: #f9fafb;
    color: #374151;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

.btn-voice-preview:hover:not(:disabled) {
    background: #eef2ff;
    border-color: #a5b4fc;
}

.btn-voice-preview:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.voice-filters {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
    margin-top: 0.5rem;
}

.voice-filter {
    padding: 0.45rem 0.5rem;
    font-size: 0.8rem;
}

.control-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1rem;
}

.directing-controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin-top: 0.25rem;
}

.director-notes-group {
    margin-top: 0.25rem;
}

.director-notes {
    min-height: 8rem;
}

.form-actions {
    display: flex;
    justify-content: flex-start;
    gap: 0.75rem;
    margin-top: 0.25rem;
    align-items: center;
}

.btn-primary {
    padding: 0.6rem 1.2rem;
    font-size: 1rem;
    font-weight: 600;
    color: #ffffff;
    background-color: #2563eb;
    border: none;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: background-color 0.15s ease-in-out;
    position: relative;
    min-width: 140px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-primary:disabled,
.btn-secondary:disabled {
    opacity: 0.7;
    cursor: not-allowed;
}

.btn-spinner {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: btn-spin 0.8s linear infinite;
    margin-left: 0.5rem;
}

.btn-secondary .btn-spinner {
    border-color: rgba(55,65,81,0.3);
    border-top-color: #374151;
}

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

.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255,255,255,0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 50;
    gap: 1rem;
}

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

.spinner-large {
    width: 3rem;
    height: 3rem;
    border: 3px solid #e5e7eb;
    border-top-color: #2563eb;
    border-radius: 50%;
    animation: btn-spin 0.9s linear infinite;
}

.loading-text {
    font-size: 1rem;
    font-weight: 500;
    color: #374151;
}

.btn-primary:active {
    background-color: #1e40af;
}

.result-section {
    background: #ffffff;
    border-radius: 0.5rem;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.hidden {
    display: none;
}

.result-title {
    font-size: 1.25rem;
    font-weight: 700;
    margin: 0 0 1rem 0;
}

.status-block {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.status-label {
    font-weight: 600;
    color: #374151;
}

.status-value {
    font-weight: 700;
    color: #2563eb;
}

.player-block {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 0.5rem;
}

.preview-selection {
    margin: 0 0 1rem 0;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    background-color: #f8fafc;
    color: #334155;
    font-size: 0.95rem;
    line-height: 1.5;
    white-space: pre-wrap;
}

.preview-actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
}

.preview-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.35rem 0.65rem;
    border-radius: 999px;
    background-color: #dcfce7;
    color: #166534;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.01em;
}

.audio-player {
    width: 100%;
    border-radius: 0.5rem;
}

.download-link {
    display: inline-block;
    padding: 0.5rem 1rem;
    font-weight: 600;
    color: #ffffff;
    background-color: #10b981;
    border-radius: 0.5rem;
    text-decoration: none;
    width: max-content;
    transition: background-color 0.15s ease-in-out;
}

.download-link:hover {
    background-color: #059669;
}

.error-block {
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    background-color: #fef2f2;
    border-left: 4px solid #ef4444;
    border-radius: 0.375rem;
}

.error-text {
    margin: 0;
    color: #b91c1c;
    font-weight: 500;
}

/* Site header */
.site-header {
    background: #ffffff;
    border-bottom: 1px solid #e5e7eb;
    padding: 0.75rem 1rem;
    position: sticky;
    top: 0;
    z-index: 10;
}

.site-nav {
    max-width: 920px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.site-logo {
    font-weight: 700;
    font-size: 1.1rem;
    color: #1a1a1a;
    text-decoration: none;
}

.user-nav {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.user-name {
    font-size: 0.9rem;
    color: #555;
}

.eleven-usage {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    min-width: 4.5rem;
    color: #166534;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: default;
}

.usage-ring {
    width: 2rem;
    height: 2rem;
    transform: rotate(-90deg);
}

.usage-ring-bg,
.usage-ring-value {
    fill: none;
    stroke-width: 4;
}

.usage-ring-bg {
    stroke: #e5e7eb;
}

.usage-ring-value {
    stroke: #22c55e;
    stroke-linecap: round;
    transition: stroke-dashoffset 0.35s ease, stroke 0.2s ease;
}

.eleven-usage-low {
    color: #b45309;
}

.eleven-usage-low .usage-ring-value {
    stroke: #f59e0b;
}

.eleven-usage-critical {
    color: #b91c1c;
}

.eleven-usage-critical .usage-ring-value {
    stroke: #ef4444;
}

.eleven-usage-unavailable {
    color: #6b7280;
}

.eleven-usage-unavailable .usage-ring-value {
    stroke: #9ca3af;
}

.logout-form {
    margin: 0;
}

.btn-logout {
    padding: 0.4rem 0.75rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: #374151;
    background-color: #f3f4f6;
    border: 1px solid #d1d5db;
    border-radius: 0.4rem;
    cursor: pointer;
    transition: background-color 0.15s ease-in-out;
}

.btn-logout:hover {
    background-color: #e5e7eb;
}

.nav-links {
    display: flex;
    gap: 1.25rem;
    margin-right: auto;
    margin-left: 1.5rem;
}
.nav-link {
    color: #555;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    transition: color 0.15s;
}
.nav-link:hover {
    color: #1a1a1a;
}

/* Login page extras */
.form-input {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    font-size: 1rem;
    background-color: #ffffff;
    color: #1a1a1a;
}

.alert {
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

.alert-error {
    background-color: #fef2f2;
    color: #b91c1c;
    border-left: 4px solid #ef4444;
}

/* History */
.history-table {
    width: 100%;
    border-collapse: collapse;
}
.history-table th,
.history-table td {
    text-align: left;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid #e5e7eb;
}
.history-table th {
    font-weight: 600;
    color: #555;
    font-size: 0.85rem;
    text-transform: uppercase;
}
.history-table td {
    font-size: 0.95rem;
    vertical-align: middle;
}
.history-table tr:last-child td {
    border-bottom: none;
}
.status-badge {
    display: inline-block;
    padding: 0.25rem 0.6rem;
    border-radius: 0.35rem;
    font-size: 0.8rem;
    font-weight: 600;
}
.status-done {
    background: #dcfce7;
    color: #166534;
}
.status-pending {
    background: #fef9c3;
    color: #854d0e;
}
.status-failed {
    background: #fee2e2;
    color: #991b1b;
}
.text-preview {
    color: #555;
    max-width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.director-preview {
    color: #374151;
    max-width: 280px;
    line-height: 1.35;
}
.date-cell {
    white-space: nowrap;
    color: #555;
    font-size: 0.85rem;
}
.link-detail {
    color: #2563eb;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
}
.link-detail:hover {
    text-decoration: underline;
}
.empty-text {
    text-align: center;
    color: #555;
}

/* Detail page */
.detail-meta {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}
.meta-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.meta-label {
    font-weight: 600;
    color: #555;
    min-width: 70px;
}
.section-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin: 0 0 0.75rem 0;
}
.text-block {
    background: #f9fafb;
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    white-space: pre-wrap;
    font-size: 0.95rem;
    line-height: 1.6;
}
.ssml-block {
    background: #1a1a1a;
    color: #e5e7eb;
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    font-family: ui-monospace, "Cascadia Code", "Fira Code", monospace;
    font-size: 0.85rem;
    line-height: 1.5;
}
.ssml-block code {
    background: none;
    color: inherit;
}
.player-actions {
    margin-top: 0.75rem;
}
.detail-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 1rem;
    flex-wrap: wrap;
}
.btn-secondary {
    display: inline-block;
    padding: 0.6rem 1.2rem;
    font-size: 1rem;
    font-weight: 600;
    color: #374151;
    background-color: #f3f4f6;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.15s ease-in-out;
}
.btn-secondary:hover {
    background-color: #e5e7eb;
}

.form-hint {
    margin: 0.5rem 0 0 0;
    font-size: 0.85rem;
    color: #555;
    line-height: 1.4;
}

.form-hint-error {
    color: #b91c1c;
}
.form-hint code {
    background: #f3f4f6;
    padding: 0.1rem 0.3rem;
    border-radius: 0.2rem;
    font-family: ui-monospace, "Cascadia Code", "Fira Code", monospace;
    font-size: 0.8rem;
}

@media (max-width: 760px) {
    .main-container {
        padding: 1rem;
    }

    .site-nav,
    .user-nav,
    .form-actions,
    .preview-actions {
        align-items: stretch;
        flex-wrap: wrap;
    }

    .control-grid {
        grid-template-columns: 1fr;
    }

    .voice-filters {
        grid-template-columns: 1fr;
    }

    .btn-primary,
    .btn-secondary {
        width: 100%;
    }
}
