@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700;1,9..40,400&family=Lora:ital,wght@0,400;0,500;0,600;1,400&display=swap');

:root {
    --color-primary: #34d399;
    --color-primary-dark: #059669;
    --color-primary-glow: rgba(52, 211, 153, 0.25);
    --color-surface: #1a1f1c;
    --color-surface-raised: #232a26;
    --color-surface-hover: #2d3630;
    --color-border: rgba(255, 255, 255, 0.06);
    --color-text: #f0f4f1;
    --color-text-muted: #8a9a90;
    --color-danger: #f87171;
    --color-accent-warm: #fbbf24;
    --radius-lg: 1.25rem;
    --radius-xl: 1.5rem;
    --shadow-soft: 0 4px 24px rgba(0, 0, 0, 0.35);
}

* {
    box-sizing: border-box;
}

body {
    font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
    background:
        radial-gradient(ellipse 80% 50% at 50% -20%, rgba(52, 211, 153, 0.12), transparent),
        radial-gradient(ellipse 60% 40% at 100% 100%, rgba(251, 191, 36, 0.06), transparent),
        #0f1210;
    color: var(--color-text);
    min-height: 100vh;
    margin: 0;
    -webkit-font-smoothing: antialiased;
}

.app-container {
    max-width: 36rem;
    margin: 0 auto;
    padding: 0 1rem 5rem;
}

.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 1rem 0 0.75rem;
    margin-bottom: 0.5rem;
    background: linear-gradient(to bottom, #0f1210 70%, transparent);
    backdrop-filter: blur(8px);
}

.header-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.875rem;
}

.brand-icon {
    width: 3rem;
    height: 3rem;
    border-radius: var(--radius-lg);
    background: linear-gradient(135deg, var(--color-primary-dark), var(--color-primary));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    color: #0f1210;
    box-shadow: 0 4px 16px var(--color-primary-glow);
    flex-shrink: 0;
}

.header-title {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin: 0;
    line-height: 1.2;
}

.header-subtitle {
    font-size: 0.8125rem;
    color: var(--color-text-muted);
    margin: 0.125rem 0 0;
}

.player-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-top: 0.875rem;
    padding: 0.625rem 0.875rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
}

.player-bar-label {
    font-size: 0.75rem;
    color: var(--color-text-muted);
    flex-shrink: 0;
}

.player-name-input {
    flex: 1;
    min-width: 8rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: 0.625rem;
    padding: 0.5rem 0.75rem;
    font-size: 0.9375rem;
    color: var(--color-text);
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.player-name-input:focus {
    border-color: rgba(52, 211, 153, 0.4);
    box-shadow: 0 0 0 3px var(--color-primary-glow);
}

.btn-set-name {
    padding: 0.5rem 0.875rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: 0.625rem;
    color: var(--color-text-muted);
    font-size: 0.8125rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, color 0.15s, transform 0.15s;
    flex-shrink: 0;
}

.btn-set-name:hover {
    background: var(--color-surface-hover);
    color: var(--color-primary);
}

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

.section-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 0.75rem;
}

.section-label i {
    color: var(--color-primary);
}

.section-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    padding: 0;
    margin: 0;
    cursor: pointer;
    color: var(--color-primary);
    font-size: inherit;
    line-height: 1;
    transition: color 0.15s, transform 0.15s;
}

.section-icon-btn:hover {
    color: var(--color-accent-warm);
}

.section-icon-btn:active {
    transform: scale(0.92);
}

.library-header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.library-section.delete-mode {
    border-top-color: rgba(248, 113, 113, 0.25);
}

.library-section.delete-mode .section-label {
    color: var(--color-danger);
}

.library-section.delete-mode .section-icon-btn {
    color: var(--color-danger);
}

.library-chip-row {
    display: flex;
    align-items: stretch;
    gap: 0.375rem;
}

.library-chip-row .library-chip {
    flex: 1;
    min-width: 0;
}

.library-delete-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    flex-shrink: 0;
    background: var(--color-surface);
    border: 1px solid rgba(248, 113, 113, 0.35);
    border-radius: var(--radius-lg);
    color: var(--color-danger);
    cursor: pointer;
    font-size: 0.875rem;
    transition: background 0.15s, border-color 0.15s, transform 0.15s;
}

.library-delete-btn:hover {
    background: rgba(248, 113, 113, 0.12);
    border-color: var(--color-danger);
}

.library-delete-btn:active {
    transform: scale(0.95);
}

.delete-mode .library-delete-btn {
    display: inline-flex;
}

.lobby-actions {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
}

.btn-primary {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    padding: 1rem 1.5rem;
    background: linear-gradient(135deg, var(--color-primary-dark), var(--color-primary));
    color: #0f1210;
    border: none;
    border-radius: var(--radius-xl);
    font-size: 1.0625rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    box-shadow: 0 4px 16px var(--color-primary-glow);
    transition: transform 0.15s, box-shadow 0.15s;
}

.btn-primary:hover {
    box-shadow: 0 6px 24px var(--color-primary-glow);
}

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

.join-form {
    display: flex;
    gap: 0.5rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-xl);
    padding: 0.375rem;
    box-shadow: var(--shadow-soft);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.join-form:focus-within {
    border-color: rgba(52, 211, 153, 0.4);
    box-shadow: var(--shadow-soft), 0 0 0 3px var(--color-primary-glow);
}

.join-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    padding: 0.875rem 1rem;
    font-size: 1.125rem;
    color: var(--color-text);
    font-family: inherit;
    text-align: center;
    letter-spacing: 0.15em;
}

.join-input::placeholder {
    color: var(--color-text-muted);
    letter-spacing: normal;
    font-size: 0.9375rem;
}

.btn-join {
    padding: 0.875rem 1.25rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    color: var(--color-text);
    font-size: 0.9375rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, transform 0.15s;
    flex-shrink: 0;
}

.btn-join:hover {
    background: var(--color-surface-hover);
}

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

.library-section {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--color-border);
}

.library-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.75rem;
}

.library-header .section-label {
    margin-bottom: 0;
}

.library-count {
    font-size: 0.6875rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    padding: 0.25rem 0.625rem;
    border-radius: 9999px;
    color: var(--color-text-muted);
}

.library-search-wrap {
    position: relative;
    margin-bottom: 0.75rem;
}

.library-search-icon {
    position: absolute;
    left: 0.875rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--color-text-muted);
    font-size: 0.8125rem;
    pointer-events: none;
}

.library-search {
    width: 100%;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 0.625rem 0.875rem 0.625rem 2.25rem;
    font-size: 0.875rem;
    color: var(--color-text);
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.library-search:focus {
    border-color: rgba(52, 211, 153, 0.4);
    box-shadow: 0 0 0 3px var(--color-primary-glow);
}

.library-search::placeholder {
    color: var(--color-text-muted);
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
}

.library-chip {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-left: 3px solid var(--color-primary);
    border-radius: var(--radius-lg);
    padding: 0.75rem 0.875rem;
    font-size: 0.8125rem;
    color: var(--color-text);
    cursor: pointer;
    text-align: left;
    font-family: inherit;
    transition: background 0.15s, border-color 0.15s, transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    width: 100%;
    animation: item-enter 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.library-chip:hover {
    background: var(--color-surface-hover);
    border-color: rgba(52, 211, 153, 0.3);
    transform: translateX(3px);
}

.library-chip-date {
    color: var(--color-accent-warm);
    font-size: 0.6875rem;
    font-weight: 600;
}

.library-chip-players {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--color-text-muted);
}

.library-chip-view {
    font-size: 0.625rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--color-primary);
    opacity: 0;
    transition: opacity 0.15s;
}

.library-chip:hover .library-chip-view {
    opacity: 1;
}

@media (hover: none) {
    .library-chip-view {
        opacity: 0.7;
    }
}

.empty-state {
    grid-column: 1 / -1;
    text-align: center;
    padding: 2.5rem 1rem;
    color: var(--color-text-muted);
}

.empty-state i {
    font-size: 2.5rem;
    color: var(--color-surface-hover);
    margin-bottom: 0.75rem;
    display: block;
}

.empty-state p {
    margin: 0;
    font-size: 0.9375rem;
    line-height: 1.5;
}

.btn-ghost {
    background: transparent;
    border: 1px solid var(--color-border);
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 600;
    padding: 0.375rem 0.75rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, color 0.15s, transform 0.15s;
}

.btn-ghost:hover {
    background: var(--color-surface-raised);
    color: var(--color-text);
}

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

.game-panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-xl);
    padding: 1.5rem;
    box-shadow: var(--shadow-soft);
}

.game-top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

.game-code-wrap {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 0.5rem 0.75rem;
}

.game-code-label {
    font-size: 0.6875rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-muted);
}

.game-code-value {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.2em;
    color: var(--color-primary);
    font-variant-numeric: tabular-nums;
}

.btn-copy-code {
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    padding: 0.375rem;
    border-radius: 0.375rem;
    font-size: 0.875rem;
    transition: color 0.15s, background 0.15s, transform 0.15s;
}

.btn-copy-code:hover {
    color: var(--color-primary);
    background: var(--color-primary-glow);
}

.btn-copy-code:active {
    transform: scale(0.95);
}

.progress-section {
    margin-bottom: 1.25rem;
}

.progress-label {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--color-text-muted);
    margin-bottom: 0.375rem;
}

.progress-track {
    height: 0.375rem;
    background: var(--color-surface-raised);
    border-radius: 9999px;
    overflow: hidden;
    border: 1px solid var(--color-border);
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--color-primary-dark), var(--color-primary));
    border-radius: 9999px;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.story-book {
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-left: 4px solid var(--color-accent-warm);
    border-radius: var(--radius-lg);
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.5rem;
    font-family: 'Lora', Georgia, serif;
    font-size: 1.0625rem;
    line-height: 1.7;
}

.story-sentence {
    margin: 0 0 0.875rem;
}

.story-sentence:last-child {
    margin-bottom: 0;
}

.sentence-enter {
    animation: sentence-enter 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes sentence-enter {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes item-enter {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.players-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.player-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.5rem 0.875rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: 9999px;
    font-size: 0.875rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.player-pill.ready {
    border-color: rgba(52, 211, 153, 0.4);
    box-shadow: 0 0 0 1px rgba(52, 211, 153, 0.15);
}

.player-pill.ready i {
    color: var(--color-primary);
}

.player-pill.waiting i {
    color: var(--color-text-muted);
}

.player-pill.current-turn {
    border-color: var(--color-accent-warm);
    box-shadow: 0 0 12px rgba(251, 191, 36, 0.25);
    animation: pulse-turn 2s ease-in-out infinite;
}

@keyframes pulse-turn {
    0%, 100% { box-shadow: 0 0 8px rgba(251, 191, 36, 0.2); }
    50% { box-shadow: 0 0 16px rgba(251, 191, 36, 0.4); }
}

.turn-prompt {
    color: var(--color-primary);
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.waiting-prompt {
    color: var(--color-accent-warm);
    font-size: 1.0625rem;
}

.sentence-input {
    width: 100%;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 1rem;
    font-size: 1rem;
    color: var(--color-text);
    font-family: inherit;
    resize: vertical;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    margin-bottom: 0.75rem;
}

.sentence-input:focus {
    border-color: rgba(52, 211, 153, 0.4);
    box-shadow: 0 0 0 3px var(--color-primary-glow);
}

.btn-action {
    padding: 0.875rem 1.5rem;
    border: none;
    border-radius: var(--radius-lg);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.15s, box-shadow 0.15s;
}

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

.btn-ready {
    background: linear-gradient(135deg, #16a34a, #22c55e);
    color: #fff;
}

.btn-ready:hover {
    box-shadow: 0 4px 16px rgba(34, 197, 94, 0.3);
}

.btn-submit {
    background: linear-gradient(135deg, var(--color-primary-dark), var(--color-primary));
    color: #0f1210;
}

.btn-submit:hover {
    box-shadow: 0 4px 16px var(--color-primary-glow);
}

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

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

.btn-purple {
    background: linear-gradient(135deg, #7c3aed, #a78bfa);
    color: #fff;
}

.finished-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--color-primary);
    margin: 0 0 1.25rem;
    text-align: center;
}

.finished-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center;
}

.link-back {
    background: none;
    border: none;
    color: var(--color-primary);
    font-size: 0.8125rem;
    cursor: pointer;
    font-family: inherit;
    padding: 0;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.story-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 50;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.story-modal.open {
    display: flex;
}

.story-modal-panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-xl);
    width: 100%;
    max-width: 32rem;
    max-height: 82vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: var(--shadow-soft), 0 8px 32px rgba(0, 0, 0, 0.5);
    animation: modal-enter 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes modal-enter {
    from {
        opacity: 0;
        transform: translateY(1rem) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.modal-header {
    padding: 1.25rem 1.5rem 1rem;
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.modal-date {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    color: var(--color-accent-warm);
    text-transform: uppercase;
}

.modal-players {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    margin-top: 0.5rem;
}

.modal-player-chip {
    font-size: 0.75rem;
    padding: 0.25rem 0.625rem;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: 9999px;
    color: var(--color-text-muted);
}

.modal-close {
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-size: 1.75rem;
    line-height: 1;
    cursor: pointer;
    padding: 0;
    transition: color 0.15s;
}

.modal-close:hover {
    color: var(--color-text);
}

.modal-story-body {
    padding: 1.25rem 1.5rem;
    overflow-y: auto;
    flex: 1;
    font-family: 'Lora', Georgia, serif;
    font-size: 1.0625rem;
    line-height: 1.75;
    color: var(--color-text);
}

.modal-story-body p {
    margin: 0 0 1rem;
}

.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--color-border);
    background: rgba(15, 18, 16, 0.6);
    text-align: right;
}

.toast-container {
    position: fixed;
    bottom: 1.25rem;
    left: 50%;
    transform: translateX(-50%);
    z-index: 60;
    width: calc(100% - 2rem);
    max-width: 28rem;
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 0.875rem 1rem;
    box-shadow: var(--shadow-soft), 0 8px 32px rgba(0, 0, 0, 0.5);
    pointer-events: auto;
    animation: toast-enter 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes toast-enter {
    from {
        opacity: 0;
        transform: translateY(1rem);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.toast-message {
    font-size: 0.875rem;
    color: var(--color-text);
}

.hidden {
    display: none !important;
}