/* ========================================
   CSS Custom Properties
   ======================================== */
:root {
    --bg: #F4F1EC;
    --surface: #FFFFFF;
    --primary: #1A6B3C;
    --primary-dark: #145530;
    --primary-light: #E8F5EE;
    --recording: #D32F2F;
    --recording-light: #FFEBEE;
    --text: #1C1C1E;
    --muted: #8E8E93;
    --border: #E5E0D8;
    --shadow: rgba(0, 0, 0, 0.08);
    --badge-prosty: #1A6B3C;
    --badge-sredni: #E65100;
    --badge-skomplikowany: #C62828;
    --bubble-user-bg: #1A6B3C;
    --bubble-user-text: #FFFFFF;
    --bubble-ai-bg: #FFFFFF;
    --bubble-ai-text: #1C1C1E;
    --bubble-ai-border: #E5E0D8;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-xl: 24px;
    --transition: 0.2s ease;
}

/* ========================================
   Reset & Base
   ======================================== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#app {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ========================================
   Typography
   ======================================== */
h1 {
    font-size: 1.75rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text);
}

h2 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text);
}

h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
}

p {
    color: var(--text);
}

/* ========================================
   Layout Primitives
   ======================================== */
.screen {
    display: flex;
    flex-direction: column;
    min-height: 100%;
    max-width: 640px;
    margin: 0 auto;
    width: 100%;
}

.screen-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.screen-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}

.screen-footer {
    padding: 16px 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

/* Language picker (apex / www landing page) */
.picker-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 32px;
    padding: 24px;
    text-align: center;
}

.picker-title {
    font-size: 1.75rem;
    font-weight: 600;
}

.picker-grid {
    display: grid;
    grid-template-columns: repeat(2, auto);
    gap: 20px;
}

.picker-flag {
    font-size: 3.5rem;
    line-height: 1;
    text-decoration: none;
    padding: 16px 20px;
    border-radius: 16px;
    transition: transform 0.1s ease, background 0.1s ease;
}

.picker-flag:hover,
.picker-flag:focus {
    background: var(--surface);
    transform: scale(1.08);
}

/* ========================================
   Buttons
   ======================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition), transform var(--transition), opacity var(--transition);
    text-decoration: none;
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.btn:active {
    transform: scale(0.97);
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
    width: 100%;
    padding: 14px 20px;
    border-radius: var(--radius-md);
    font-size: 1.05rem;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
}

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

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

.btn-icon {
    padding: 10px;
    border-radius: 50%;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text);
    transition: background var(--transition);
    -webkit-tap-highlight-color: transparent;
    font-size: 1.2rem;
}

.btn-icon:hover {
    background: var(--bg);
}

/* ========================================
   Forms
   ======================================== */
.form-group {
    margin-bottom: 16px;
}

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
}

.form-label .required {
    color: var(--recording);
    margin-left: 2px;
}

.form-input,
.form-select {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color var(--transition), box-shadow var(--transition);
    outline: none;
    appearance: none;
    -webkit-appearance: none;
}

.form-input:focus,
.form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(26, 107, 60, 0.15);
}

.form-input::placeholder {
    color: var(--muted);
}

.form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238E8E93' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

.form-hint {
    font-size: 0.8rem;
    color: var(--muted);
    margin-top: 4px;
}

/* ========================================
   Cards
   ======================================== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 12px;
    box-shadow: 0 1px 4px var(--shadow);
}

.card-clickable {
    cursor: pointer;
    transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
    -webkit-tap-highlight-color: transparent;
}

.card-clickable:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px var(--shadow);
    border-color: var(--primary);
}

.card-clickable:active {
    transform: translateY(0);
}

/* ========================================
   Avatar
   ======================================== */
.avatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    font-weight: 700;
    flex-shrink: 0;
    text-transform: uppercase;
}

.avatar-lg {
    width: 44px;
    height: 44px;
    font-size: 1.2rem;
}

/* ========================================
   Level Badges
   ======================================== */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 100px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.badge-prosty {
    background: #E8F5EE;
    color: var(--badge-prosty);
}

.badge-sredni {
    background: #FFF3E0;
    color: var(--badge-sredni);
}

.badge-skomplikowany {
    background: #FFEBEE;
    color: var(--badge-skomplikowany);
}

/* ========================================
   Setup Screen
   ======================================== */
.setup-screen {
    background: var(--bg);
}

.setup-hero {
    text-align: center;
    padding: 40px 20px 24px;
}

.setup-hero h1 {
    font-size: 2rem;
    margin-bottom: 8px;
}

.setup-hero p {
    color: var(--muted);
    font-size: 0.95rem;
}

.setup-section-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin: 24px 0 12px;
    padding: 0 20px;
}

.setup-body {
    padding: 0 20px 100px;
}

.persona-setup-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 16px;
}

.persona-setup-card h3 {
    margin-bottom: 16px;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.persona-setup-card h3 .persona-number {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 700;
    flex-shrink: 0;
}

.persona-setup-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 16px;
}

.persona-setup-head h3 {
    margin-bottom: 0;
}

.btn-losuj {
    flex-shrink: 0;
    padding: 7px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--text);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition), transform var(--transition);
    -webkit-tap-highlight-color: transparent;
}

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

.btn-losuj:active {
    transform: scale(0.96);
}

.setup-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px 20px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    max-width: 640px;
    margin: 0 auto;
}

/* ========================================
   Home Screen
   ======================================== */
.home-screen {
    background: var(--bg);
}

.home-header {
    padding: 20px 20px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.home-header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 4px;
}

.home-header h1 {
    font-size: 1.5rem;
}

.home-header-actions {
    display: flex;
    gap: 4px;
}

.home-subtitle {
    font-size: 0.875rem;
    color: var(--muted);
}

.home-body {
    padding: 20px;
}

.persona-card {
    display: flex;
    align-items: flex-start;
    gap: 14px;
}

.persona-card-info {
    flex: 1;
    min-width: 0;
}

.persona-card-name {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 4px;
}

.persona-card-desc {
    font-size: 0.875rem;
    color: var(--muted);
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.persona-card-footer {
    display: flex;
    align-items: center;
    gap: 8px;
}

.persona-card-voice {
    font-size: 0.75rem;
    color: var(--muted);
}

.persona-card-arrow {
    color: var(--muted);
    font-size: 1.2rem;
    flex-shrink: 0;
    align-self: center;
}

/* ========================================
   Conversation Screen
   ======================================== */
.conv-screen {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--bg);
}

.conv-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.conv-header-info {
    flex: 1;
    min-width: 0;
}

.conv-header-name {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-header-sub {
    font-size: 0.8rem;
    color: var(--muted);
}

.conv-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
}

.conv-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    padding-bottom: calc(32px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    gap: 16px;
}

/* ========================================
   Chat Bubbles
   ======================================== */
.bubble-row {
    display: flex;
    flex-direction: column;
}

.bubble-row-user {
    align-items: flex-end;
}

.bubble-row-ai {
    align-items: flex-start;
}

.bubble {
    max-width: 80%;
    padding: 12px 16px;
    border-radius: var(--radius-xl);
    font-size: 0.975rem;
    line-height: 1.5;
    word-break: break-word;
}

.bubble-user {
    background: var(--bubble-user-bg);
    color: var(--bubble-user-text);
    border-bottom-right-radius: 6px;
}

.bubble-ai {
    background: var(--bubble-ai-bg);
    color: var(--bubble-ai-text);
    border: 1px solid var(--bubble-ai-border);
    border-bottom-left-radius: 6px;
}

.bubble-correction {
    border-left: 3px solid var(--recording);
    background: #FFF9F9;
}

.bubble-role-label {
    font-size: 0.72rem;
    color: var(--muted);
    margin-bottom: 3px;
    padding: 0 4px;
}

/* ========================================
   Mic Button
   ======================================== */
.mic-btn {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    border: none;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: none;
    box-shadow: 0 4px 16px rgba(26, 107, 60, 0.35);
    font-size: 1.8rem;
    position: relative;
}

.mic-btn:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: scale(1.05);
}

.mic-btn:disabled {
    background: #C8C8CC;
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
}

.mic-btn.recording {
    background: var(--recording);
    box-shadow: 0 4px 16px rgba(211, 47, 47, 0.4);
    animation: pulse-ring 1.2s ease-out infinite;
}

.mic-btn.recording:hover {
    background: var(--recording);
}

@keyframes pulse-ring {
    0% {
        box-shadow: 0 0 0 0 rgba(211, 47, 47, 0.5), 0 4px 16px rgba(211, 47, 47, 0.4);
    }
    70% {
        box-shadow: 0 0 0 16px rgba(211, 47, 47, 0), 0 4px 16px rgba(211, 47, 47, 0.4);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(211, 47, 47, 0), 0 4px 16px rgba(211, 47, 47, 0.4);
    }
}

.conv-status {
    font-size: 0.85rem;
    color: var(--muted);
    text-align: center;
    padding: 4px 0;
    min-height: 24px;
}

/* ========================================
   History Screen
   ======================================== */
.history-session {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    margin-bottom: 12px;
    overflow: hidden;
}

.history-session-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.history-session-header:hover {
    background: var(--bg);
}

.history-session-meta {
    flex: 1;
    min-width: 0;
}

.history-session-title {
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-session-date {
    font-size: 0.8rem;
    color: var(--muted);
    margin-top: 2px;
}

.history-session-body {
    border-top: 1px solid var(--border);
    padding: 12px 16px;
    display: none;
    background: var(--bg);
}

.history-session-body.open {
    display: block;
}

.history-msg {
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
    line-height: 1.5;
}

.history-msg:last-child {
    border-bottom: none;
}

.history-msg-role {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin-bottom: 2px;
}

.history-msg-role.user {
    color: var(--primary);
}

.history-msg-role.ai {
    color: var(--muted);
}

.history-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.history-delete-btn {
    background: none;
    border: none;
    color: var(--recording);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    transition: background var(--transition);
    -webkit-tap-highlight-color: transparent;
}

.history-delete-btn:hover {
    background: var(--recording-light);
}

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

.empty-state {
    text-align: center;
    padding: 60px 20px;
}

.empty-state-icon {
    font-size: 3rem;
    margin-bottom: 16px;
    opacity: 0.4;
}

.empty-state h3 {
    color: var(--muted);
    font-weight: 500;
    margin-bottom: 6px;
}

.empty-state p {
    font-size: 0.875rem;
    color: var(--muted);
}

.history-session-toggle {
    font-size: 0.85rem;
    color: var(--muted);
    margin-left: 8px;
    flex-shrink: 0;
}

/* ========================================
   Settings Screen
   ======================================== */
.settings-section {
    margin-bottom: 28px;
}

.settings-section-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    margin-bottom: 12px;
}

.settings-persona-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
    margin-bottom: 12px;
}

.settings-persona-card h3 {
    font-size: 0.9rem;
    color: var(--primary);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.success-msg {
    background: var(--primary-light);
    color: var(--primary);
    border: 1px solid #A8D5B8;
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    margin-top: 12px;
    animation: fade-in 0.2s ease;
}

.error-msg {
    background: var(--recording-light);
    color: var(--recording);
    border: 1px solid #FFCDD2;
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-size: 0.9rem;
    text-align: center;
    margin-top: 12px;
}

@keyframes fade-in {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ========================================
   Thinking indicator
   ======================================== */
.thinking-bubble {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: var(--bubble-ai-bg);
    border: 1px solid var(--bubble-ai-border);
    border-radius: var(--radius-xl);
    border-bottom-left-radius: 6px;
    max-width: 80%;
}

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

.thinking-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted);
    animation: dot-bounce 1.2s ease-in-out infinite;
}

.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

/* ========================================
   Utility classes
   ======================================== */
.mb-0 { margin-bottom: 0; }

/* ========================================
   Scrollbar
   ======================================== */
::-webkit-scrollbar {
    width: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

/* ========================================
   Responsive
   ======================================== */
@media (min-width: 640px) {
    .screen {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }

    .setup-footer {
        left: 50%;
        transform: translateX(-50%);
        width: 640px;
    }

    h1 {
        font-size: 2rem;
    }
}

/* ========================================
   App version row
   ======================================== */
.app-version-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.app-version-label {
    font-size: 0.9rem;
    color: var(--muted);
}

.app-version-value {
    font-size: 0.9rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}

/* ========================================
   Memory ("Wiedza o mnie") text
   ======================================== */
.memory-text {
    white-space: pre-wrap;
    font-size: 0.92rem;
    line-height: 1.5;
    color: var(--text);
}

/* ========================================
   About screen
   ======================================== */
.about-card h3 {
    font-size: 0.95rem;
    margin: 18px 0 6px;
    color: var(--primary);
}
.about-card h3:first-child {
    margin-top: 0;
}
.about-card p {
    font-size: 0.9rem;
    line-height: 1.55;
    color: var(--text);
    margin: 0 0 4px;
}
.about-card ol {
    margin: 0 0 4px;
    padding-left: 20px;
}
.about-card li {
    font-size: 0.9rem;
    line-height: 1.55;
    color: var(--text);
    margin-bottom: 4px;
}
