:root {
    color-scheme: light;
    --bg: #f4f6f2;
    --panel: #ffffff;
    --text: #16211c;
    --muted: #65736b;
    --line: #dce3dc;
    --accent: #11664f;
    --accent-strong: #0a4b3a;
    --blue: #315f7d;
    --danger: #a93e42;
    --danger-bg: #fff0f0;
    --success: #176b3a;
    --success-bg: #edf8f0;
    --warn: #c18116;
    --shadow: 0 18px 40px rgba(24, 33, 28, 0.08);
}

* {
    box-sizing: border-box;
}

html {
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 16px;
}

body {
    min-height: 100vh;
    margin: 0;
    color: var(--text);
    background:
        linear-gradient(180deg, rgba(17, 102, 79, 0.08), transparent 22rem),
        var(--bg);
}

button,
input,
textarea,
select {
    font: inherit;
}

button {
    min-height: 2.6rem;
    border: 0;
    border-radius: 8px;
    padding: 0.65rem 0.95rem;
    color: #fff;
    background: var(--accent);
    cursor: pointer;
    transition: transform 0.14s ease, background 0.14s ease, box-shadow 0.14s ease;
}

button:hover {
    background: var(--accent-strong);
    transform: translateY(-1px);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
    transform: none;
}

button.secondary {
    color: var(--text);
    background: #eef3ef;
}

button.secondary:hover {
    background: #e0e9e2;
}

button.danger {
    color: var(--danger);
    background: var(--danger-bg);
}

button.danger:hover {
    color: #fff;
    background: var(--danger);
}

button.success {
    color: #fff;
    background: var(--success);
}

button.success:hover {
    background: #10532d;
}

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.75rem 0.8rem;
    color: var(--text);
    background: #fff;
}

textarea {
    resize: vertical;
}

label {
    display: block;
    margin-bottom: 0.35rem;
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 700;
}

.app-shell {
    width: min(1480px, 100%);
    margin: 0 auto;
    padding: 1rem;
}

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

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

.brand-mark {
    display: grid;
    width: 2.8rem;
    height: 2.8rem;
    place-items: center;
    border-radius: 8px;
    color: #fff;
    background: var(--accent);
    font-weight: 800;
}

.brand strong,
.brand span {
    display: block;
}

.brand strong {
    font-size: 1.1rem;
}

.brand span {
    color: var(--muted);
    font-size: 0.92rem;
}

.top-actions {
    display: flex;
    align-items: end;
    gap: 0.6rem;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.top-actions .select-label {
    width: auto;
    margin: 0;
    align-self: center;
}

.top-actions select {
    width: auto;
    min-width: 7rem;
}

.main-grid {
    display: grid;
    grid-template-columns: minmax(230px, 0.8fr) minmax(350px, 1.4fr) minmax(330px, 1fr);
    gap: 1rem;
    align-items: start;
}

.panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: var(--shadow);
}

.panel-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
}

.panel-heading h1,
.panel-heading h2 {
    margin: 0;
    font-size: clamp(1.25rem, 2vw, 1.6rem);
    line-height: 1.15;
}

.panel-heading.split {
    align-items: center;
}

.eyebrow {
    margin: 0 0 0.25rem;
    color: var(--blue);
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0;
    text-transform: uppercase;
}

.inline-form {
    margin-bottom: 1rem;
}

.inline-row {
    display: flex;
    gap: 0.45rem;
}

.inline-row input {
    min-width: 0;
}

.inline-row button {
    flex: 0 0 auto;
}

.deck-list {
    display: grid;
    gap: 0.55rem;
}

.deck-item {
    width: 100%;
    display: flex;
    justify-content: space-between;
    gap: 0.7rem;
    border: 1px solid var(--line);
    color: var(--text);
    background: #fff;
    text-align: left;
}

.deck-item:hover,
.deck-item.active {
    color: #fff;
    background: var(--accent);
    border-color: var(--accent);
}

.deck-item span {
    overflow-wrap: anywhere;
}

.deck-item small {
    flex: 0 0 auto;
    opacity: 0.8;
}

.empty-state {
    border: 1px dashed var(--line);
    border-radius: 8px;
    padding: 1rem;
    color: var(--muted);
    background: #fbfcfb;
}

.deck-tools {
    display: grid;
    gap: 0.65rem;
    margin-bottom: 1rem;
}

.counter {
    min-width: 4.8rem;
    border-radius: 999px;
    padding: 0.45rem 0.7rem;
    color: var(--accent);
    background: #e8f4ed;
    font-weight: 800;
    text-align: center;
}

.editor-heading-actions,
.study-heading-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.study-now-btn {
    min-height: 2.25rem;
    padding: 0.45rem 0.75rem;
}

.limit-track {
    width: 100%;
    height: 0.55rem;
    overflow: hidden;
    border-radius: 999px;
    background: #e7ece7;
}

.limit-track span {
    display: block;
    width: 0;
    height: 100%;
    border-radius: inherit;
    background: var(--warn);
    transition: width 0.2s ease;
}

.card-form {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.field {
    min-width: 0;
}

.span-two {
    grid-column: 1 / -1;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

.cards-list {
    display: grid;
    gap: 0.65rem;
}

.card-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.75rem;
    align-items: start;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.8rem;
    background: #fff;
}

.card-row strong,
.card-row span,
.card-row small {
    display: block;
}

.card-row strong,
.card-row span {
    overflow-wrap: anywhere;
}

.card-row span {
    margin-top: 0.2rem;
    color: var(--blue);
    font-weight: 700;
}

.card-row small {
    margin-top: 0.35rem;
    color: var(--muted);
    line-height: 1.4;
}

.row-actions {
    display: flex;
    gap: 0.35rem;
}

.row-actions button {
    min-height: 2.1rem;
    padding: 0.35rem 0.55rem;
}

.study-panel {
    position: sticky;
    top: 1rem;
}

.study-toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    margin: 0 0 0.75rem;
}

.icon-toggle {
    position: relative;
    display: inline-flex;
    width: 2.6rem;
    min-height: 2.6rem;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    padding: 0;
    color: var(--text);
    background: #eef3ef;
}

.icon-toggle:hover,
.icon-toggle.active {
    color: #fff;
    border-color: var(--accent);
    background: var(--accent);
}

.icon-toggle.muted {
    color: var(--danger);
    background: #fff;
}

.icon-toggle.muted::after {
    content: "";
    position: absolute;
    width: 1.75rem;
    height: 2px;
    border-radius: 999px;
    background: currentColor;
    transform: rotate(-42deg);
}

.icon-toggle.wide {
    width: auto;
    gap: 0.4rem;
    padding: 0 0.75rem;
}

.icon-toggle svg,
.speak-card-btn svg {
    width: 1.25rem;
    height: 1.25rem;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

#directionLabel {
    font-size: 0.9rem;
    font-weight: 800;
    white-space: nowrap;
}

.study-stats {
    min-height: 1.4rem;
    margin-bottom: 0.75rem;
    color: var(--muted);
    font-weight: 700;
}

.flashcard {
    position: relative;
    width: 100%;
    min-height: 19rem;
    border: 1px solid #cad8d0;
    border-radius: 8px;
    color: var(--text);
    background: #fbfffc;
    text-align: center;
}

.flashcard-face {
    display: grid;
    width: 100%;
    min-height: 19rem;
    place-items: center;
    align-content: center;
    gap: 0.7rem;
    border: 0;
    border-radius: 8px;
    padding: 1.5rem;
    color: var(--text);
    background: transparent;
    text-align: center;
}

.flashcard-face:hover {
    color: var(--text);
    background: #f2fbf4;
}

.flashcard-face strong {
    width: 100%;
    font-size: clamp(1.7rem, 5vw, 2.7rem);
    line-height: 1.12;
    overflow-wrap: anywhere;
}

.flashcard-face small {
    max-width: 100%;
    color: var(--muted);
    font-size: 1rem;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.card-language {
    border-radius: 999px;
    padding: 0.35rem 0.65rem;
    color: var(--blue);
    background: #eaf2f7;
    font-size: 0.85rem;
    font-weight: 800;
}

.speak-card-btn {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    z-index: 2;
    display: inline-flex;
    width: 2.6rem;
    min-height: 2.6rem;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0;
    color: var(--accent);
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 8px 22px rgba(24, 33, 28, 0.12);
}

.speak-card-btn:hover {
    color: #fff;
    border-color: var(--accent);
    background: var(--accent);
}

.study-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.55rem;
    margin-top: 0.75rem;
}

.toast {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    max-width: min(28rem, calc(100vw - 2rem));
    border-radius: 8px;
    padding: 0.85rem 1rem;
    color: #fff;
    background: #1c2a22;
    box-shadow: var(--shadow);
    opacity: 0;
    transform: translateY(1rem);
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
}

body.study-mode {
    background: #eef4ef;
}

body.study-mode .app-shell {
    width: min(820px, 100%);
    min-height: 100vh;
    padding: clamp(0.75rem, 2vw, 1.5rem);
}

body.study-mode .topbar,
body.study-mode .deck-panel,
body.study-mode .editor-panel {
    display: none;
}

body.study-mode .main-grid {
    display: block;
}

body.study-mode .study-panel {
    position: static;
    min-height: calc(100vh - 2rem);
    border: 0;
    padding: clamp(0.85rem, 2.5vw, 1.25rem);
    background: transparent;
    box-shadow: none;
}

body.study-mode .study-panel .panel-heading {
    margin-bottom: 0.55rem;
}

body.study-mode .study-stats {
    text-align: center;
}

body.study-mode .flashcard,
body.study-mode .flashcard-face {
    min-height: min(58vh, 28rem);
}

body.study-mode .flashcard {
    box-shadow: 0 18px 45px rgba(24, 33, 28, 0.1);
}

body.study-mode .flashcard-face {
    padding: 1.8rem 1.2rem;
}

body.study-mode .flashcard-face strong {
    font-size: clamp(2.05rem, 9vw, 4rem);
}

body.study-mode .study-actions {
    max-width: 34rem;
    margin: 0.85rem auto 0;
}

@media (max-width: 1100px) {
    .main-grid {
        grid-template-columns: minmax(230px, 0.75fr) minmax(350px, 1.25fr);
    }

    .study-panel {
        position: static;
        grid-column: 1 / -1;
    }
}

@media (max-width: 760px) {
    .app-shell {
        padding: 0.75rem;
    }

    .topbar,
    .top-actions {
        align-items: stretch;
    }

    .topbar {
        flex-direction: column;
    }

    .top-actions {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        width: 100%;
    }

    .top-actions .select-label {
        grid-column: 1 / -1;
    }

    .top-actions select {
        width: 100%;
    }

    .main-grid,
    .card-form,
    .card-row {
        grid-template-columns: 1fr;
    }

    .panel {
        padding: 0.85rem;
    }

    .panel-heading.split {
        align-items: flex-start;
        flex-direction: column;
    }

    .editor-heading-actions,
    .study-heading-actions {
        width: 100%;
        justify-content: space-between;
    }

    .inline-row,
    .form-actions {
        flex-wrap: wrap;
    }

    .inline-row button,
    .form-actions button {
        flex: 1 1 auto;
    }

    .row-actions {
        justify-content: stretch;
    }

    .row-actions button {
        flex: 1 1 auto;
    }

    .flashcard {
        min-height: 16rem;
    }

    .flashcard-face {
        min-height: 16rem;
        padding: 1.2rem;
    }

    .study-toolbar {
        justify-content: flex-start;
        overflow-x: auto;
        padding-bottom: 0.1rem;
    }

    body.study-mode .study-panel .panel-heading {
        flex-direction: row;
        align-items: center;
    }

    body.study-mode .study-heading-actions {
        width: auto;
        justify-content: flex-end;
    }

    body.study-mode .study-toolbar {
        justify-content: center;
    }

    body.study-mode .flashcard,
    body.study-mode .flashcard-face {
        min-height: min(54vh, 24rem);
    }
}
