:root {
    --bg: #f6efe7;
    --bg-scene:
        radial-gradient(circle at top left, rgba(215, 123, 61, 0.18), transparent 28%),
        radial-gradient(circle at top right, rgba(24, 114, 93, 0.14), transparent 24%),
        linear-gradient(180deg, #fdf8f3 0%, #f7f0e7 54%, #efe7dd 100%);
    --surface: rgba(255, 251, 247, 0.9);
    --surface-strong: rgba(255, 254, 251, 0.98);
    --surface-soft: #fbf4ec;
    --line: rgba(87, 58, 36, 0.12);
    --text: #2d221b;
    --text-soft: #756458;
    --brand: #cb7138;
    --brand-strong: #9e4c20;
    --teal: #1b6f5e;
    --danger: #cf5143;
    --shadow-xl: 0 28px 60px rgba(66, 44, 27, 0.14);
    --shadow-lg: 0 18px 36px rgba(66, 44, 27, 0.1);
    --shadow-md: 0 10px 24px rgba(66, 44, 27, 0.08);
    --radius-xl: 32px;
    --radius-lg: 24px;
    --radius-md: 18px;
    --radius-sm: 14px;
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    margin: 0;
    min-height: 100vh;
    padding: 18px;
    color: var(--text);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-scene);
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
        linear-gradient(rgba(158, 76, 32, 0.05) 1px, transparent 1px),
        linear-gradient(90deg, rgba(158, 76, 32, 0.05) 1px, transparent 1px);
    background-size: 42px 42px;
    mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.26), transparent 78%);
}

.container {
    position: relative;
    max-width: 1660px;
    min-height: calc(100vh - 36px);
    margin: 0 auto;
    overflow: hidden;
    border: 1px solid rgba(255, 248, 241, 0.88);
    border-radius: var(--radius-xl);
    background: linear-gradient(180deg, rgba(255, 252, 248, 0.98), rgba(248, 242, 235, 0.94));
    box-shadow: var(--shadow-xl);
    backdrop-filter: blur(12px);
}

header {
    position: relative;
    padding: 38px 34px 30px;
    text-align: center;
    color: #fff7f2;
    background:radial-gradient(circle at left top, rgba(255, 239, 225, 0.28), transparent 24%), radial-gradient(circle at right top, rgba(255, 203, 164, 0.2), transparent 22%), linear-gradient(135deg, #472413 0%, #409b40 46%, #409b40 100%)
}

header::after {
    content: '';
    position: absolute;
    inset: auto 0 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 239, 225, 0.5), transparent);
}

header h1 {
    margin: 0;
    font-family: Georgia, 'Times New Roman', serif;
    font-size: clamp(2.2rem, 3vw, 3.4rem);
    line-height: 1.04;
    letter-spacing: -0.05em;
}

header .subtitle {
    max-width: 760px;
    margin: 12px auto 0;
    font-size: 1rem;
    line-height: 1.65;
    color: rgba(255, 245, 237, 0.9);
}

.page-nav {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 12px;
    margin-top: 24px;
    padding-top: 18px;
    border-top: 1px solid rgba(255, 236, 220, 0.24);
}

.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 10px 18px;
    border: 1px solid rgba(255, 242, 232, 0.16);
    border-radius: 999px;
    background: rgba(255, 244, 235, 0.12);
    color: #fff8f3;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    transition: transform 0.18s ease, background 0.18s ease, border-color 0.18s ease;
}

.page-link:hover {
    transform: translateY(-1px);
    background: rgba(255, 241, 231, 0.2);
    border-color: rgba(255, 241, 231, 0.3);
}

.page-link.active {
    background: rgba(255, 252, 248, 0.98);
    color: var(--brand-strong);
    border-color: rgba(255, 246, 239, 0.95);
}

.page-link.page-link-secondary {
    background: rgba(255, 244, 235, 0.08);
}

.tab-navigation {
    display: none;
}

.stats-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
    padding: 26px 28px 0;
}

.stat {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: linear-gradient(180deg, rgba(255, 253, 249, 0.95), rgba(250, 243, 236, 0.86));
    box-shadow: var(--shadow-md);
    color: var(--text-soft);
    font-size: 0.88rem;
    border-top: 4px solid rgba(203, 113, 56, 0.28);
}

.stat span {
    margin-left: 6px;
    color: var(--brand-strong);
    font-weight: 800;
}

.ui-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    margin-left: 0;
    font-size: 0.9rem;
    line-height: 1;
}

.ui-icon-heading {
    font-size: 0.95rem;
}

.main-wrap {
    display: flex;
    gap: 24px;
    padding: 24px 28px 30px;
}

.text-section {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 18px;
}

.button-section {
    flex: 0 0 430px;
    width: 430px;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    max-height: 650px;
    overflow-y: auto;
    padding-right: 6px;
}

.button-section::-webkit-scrollbar {
    width: 8px;
}

.button-section::-webkit-scrollbar-thumb {
    border-radius: 999px;
    background: rgba(27, 111, 94, 0.24);
}

.find-replace,
.editor,
.category,
.downloads,
.action-buttons {
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-md);
}

.find-replace {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 10px;
    padding: 12px;
}

.editor {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 14px;
    padding: 14px;
}

.input-editor-wrap {
    display: grid;
    gap: 12px;
    min-width: 0;
}

.input-editor-host {
    display: none;
}

.input-editor-wrap.is-enhanced #inputText {
    display: none;
}

.input-editor-wrap.is-enhanced .input-editor-host {
    display: grid;
    gap: 0;
    min-height: 360px;
    border: 1px solid rgba(87, 58, 36, 0.14);
    border-radius: 18px;
    overflow: hidden;
    background: linear-gradient(180deg, #fffdfa 0%, #f8efe7 100%);
}

.input-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 12px;
    border: 1px solid rgba(87, 58, 36, 0.12);
    border-radius: 18px;
    background: linear-gradient(180deg, rgba(255, 252, 248, 0.96), rgba(248, 240, 231, 0.9));
}

.secondary-btn {
    min-height: 40px;
    padding: 10px 14px;
    background: linear-gradient(135deg, rgba(27, 111, 94, 0.96) 0%, rgba(20, 86, 73, 1) 100%);
    box-shadow: 0 10px 22px rgba(27, 111, 94, 0.18);
}

.chain-toggle {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    user-select: none;
    color: var(--text);
    font-size: 0.88rem;
    font-weight: 700;
}

.chain-toggle input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.chain-toggle-ui {
    position: relative;
    width: 58px;
    height: 32px;
    flex: 0 0 auto;
    border-radius: 999px;
    background: linear-gradient(135deg, rgba(135, 120, 107, 0.32) 0%, rgba(87, 58, 36, 0.18) 100%);
    box-shadow: inset 0 0 0 1px rgba(87, 58, 36, 0.14);
    transition: background 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.chain-toggle-ui::after {
    content: '';
    position: absolute;
    top: 4px;
    left: 4px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #fffdfa;
    box-shadow: 0 6px 14px rgba(66, 44, 27, 0.22);
    transition: transform 0.2s ease;
}

.chain-toggle-copy {
    color: var(--text-soft);
    transition: color 0.2s ease;
}

.chain-toggle input:checked + .chain-toggle-ui {
    background: linear-gradient(135deg, var(--teal) 0%, #238f79 100%);
    box-shadow: inset 0 0 0 1px rgba(27, 111, 94, 0.2), 0 8px 18px rgba(27, 111, 94, 0.18);
}

.chain-toggle input:checked + .chain-toggle-ui::after {
    transform: translateX(26px);
}

.chain-toggle input:checked ~ .chain-toggle-copy {
    color: var(--text);
}

.chain-toggle input:focus-visible + .chain-toggle-ui {
    outline: 0;
    box-shadow: 0 0 0 4px rgba(27, 111, 94, 0.16);
}

.input-toolbar.ql-toolbar.ql-snow,
.output-toolbar.ql-toolbar.ql-snow {
    border: 0;
    border-bottom: 1px solid rgba(87, 58, 36, 0.12);
    background: rgba(255, 251, 247, 0.92);
}

.input-editor-surface,
.output-editor-surface {
    min-height: 300px;
    background: linear-gradient(180deg, #fffdfa 0%, #f8efe7 100%);
}

.input-editor-surface .ql-editor,
.output-editor-surface .ql-editor {
    min-height: 300px;
    padding: 18px;
    color: var(--text);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-size: 0.95rem;
    line-height: 1.6;
}

.input-editor-host .ql-container.ql-snow,
.output-editor-host .ql-container.ql-snow {
    border: 0;
    font-family: inherit;
}

.input-editor-wrap.is-enhanced:focus-within .input-editor-host,
.output-editor-wrap.is-enhanced:focus-within .output-editor-host {
    border-color: rgba(203, 113, 56, 0.44);
    box-shadow: 0 0 0 4px rgba(203, 113, 56, 0.12);
}

.input-editor-host .ql-editor.ql-blank::before,
.output-editor-host .ql-editor.ql-blank::before {
    left: 18px;
    right: 18px;
    color: rgba(117, 100, 88, 0.8);
    font-style: normal;
}

textarea,
input {
    width: 100%;
    color: var(--text);
    border: 1px solid rgba(87, 58, 36, 0.14);
    border-radius: 18px;
    background: var(--surface-strong);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-size: 0.95rem;
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

textarea {
    min-height: 360px;
    padding: 18px;
    line-height: 1.6;
    resize: vertical;
}

.output-editor-wrap {
    min-width: 0;
}

.output-editor-host {
    display: none;
}

.output-editor-wrap.is-enhanced #outputText {
    display: none;
}

.output-editor-wrap.is-enhanced .output-editor-host {
    display: grid;
    gap: 0;
    min-height: 360px;
    border: 1px solid rgba(87, 58, 36, 0.14);
    border-radius: 18px;
    overflow: hidden;
    background: linear-gradient(180deg, #fffdfa 0%, #f8efe7 100%);
}

input {
    padding: 12px 14px;
}

textarea:focus,
input:focus {
    outline: none;
    border-color: rgba(203, 113, 56, 0.44);
    box-shadow: 0 0 0 4px rgba(203, 113, 56, 0.12);
}

#outputText {
    background: linear-gradient(180deg, #fffdfa 0%, #f8efe7 100%);
}

.copy-btn {
    position: absolute;
    top: 24px;
    right: 26px;
    gap: 6px;
    min-height: 36px;
    padding: 8px 12px;
    font-size: 0.78rem;
}

.action-buttons,
.downloads {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
    gap: 10px;
    padding: 12px;
}

.category {
    padding: 16px;
}

.category h3 {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0 0 14px;
    color: var(--text);
    font-size: 0.82rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.category button {
    width: 100%;
    margin-top: 10px;
    text-align: left;
    justify-content: flex-start;
}

button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    padding: 11px 15px;
    border: 0;
    border-radius: 14px;
    background: linear-gradient(135deg, #409b40 0%, #409b40 100%);
    color: white;
    font-size: 0.88rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 12px 24px rgba(158, 76, 32, 0.2);
    transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
}

button:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 30px rgba(158, 76, 32, 0.24);
}

button.danger {
    background: linear-gradient(135deg, var(--danger) 0%, #a93c2f 100%);
}

.diary-shell {
    padding: 28px;
}

.diary-layout {
    display: grid;
    grid-template-columns: minmax(0, 320px) minmax(0, 1fr);
    gap: 24px;
    align-items: start;
}

.diary-card {
    border: 1px solid rgba(87, 58, 36, 0.12);
    border-radius: 24px;
    background: linear-gradient(180deg, rgba(255, 253, 249, 0.98), rgba(248, 241, 234, 0.92));
    box-shadow: 0 12px 24px rgba(66, 44, 27, 0.08);
}

.diary-sidebar,
.diary-editor-card,
.diary-help {
    padding: 22px;
}

.diary-sidebar h2,
.diary-editor-card h2,
.diary-help h3 {
    margin: 0 0 14px;
    color: #2d221b;
    font-family: Georgia, 'Times New Roman', serif;
}

.diary-copy,
.diary-help li,
.diary-meta {
    color: #756458;
    line-height: 1.7;
}

.diary-form,
.diary-field {
    display: grid;
    gap: 14px;
}

.diary-field {
    gap: 8px;
}

.diary-field label {
    color: #4b382d;
    font-size: 0.9rem;
    font-weight: 700;
}

.diary-input,
.diary-date {
    width: 100%;
    min-height: 46px;
    padding: 12px 14px;
    border: 1px solid rgba(87, 58, 36, 0.16);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.9);
    color: #2d221b;
    font: inherit;
}

.diary-actions,
.diary-downloads {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.diary-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 46px;
    padding: 12px 18px;
    border: 0;
    border-radius: 999px;
    cursor: pointer;
    font: inherit;
    font-weight: 700;
}

.diary-btn-primary {
    background: linear-gradient(135deg, #cb7138, #9e4c20);
    color: #fffaf5;
}

.diary-btn-secondary {
    border: 1px solid rgba(27, 111, 94, 0.14);
    background: rgba(27, 111, 94, 0.1);
    color: #1b6f5e;
}

.diary-btn-ghost {
    background: rgba(87, 58, 36, 0.08);
    color: #4b382d;
}

.diary-status {
    min-height: 24px;
    font-size: 0.95rem;
    font-weight: 600;
}

.diary-status[data-tone="success"] {
    color: #1f7a3f;
}

.diary-status[data-tone="error"] {
    color: #b43e33;
}

.diary-status[data-tone="info"] {
    color: #1b6f5e;
}

.diary-toolbar {
    border: 1px solid rgba(87, 58, 36, 0.12);
    border-bottom: 0;
    border-radius: 18px 18px 0 0;
    background: rgba(255, 252, 248, 0.94);
}

.diary-editor {
    min-height: 420px;
    border: 1px solid rgba(87, 58, 36, 0.12);
    border-radius: 0 0 18px 18px;
    background: rgba(255, 255, 255, 0.96);
}

.diary-fallback-editor {
    min-height: 420px;
    padding: 18px;
    border: 1px solid rgba(87, 58, 36, 0.12);
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.96);
    color: #2d221b;
    font: inherit;
    line-height: 1.7;
    resize: vertical;
}

.diary-fallback-editor[hidden],
.diary-editor[hidden] {
    display: none;
}

.diary-editor .ql-editor {
    min-height: 420px;
    padding: 18px;
    color: #2d221b;
    font-size: 1rem;
    line-height: 1.7;
}

.diary-editor .ql-editor.ql-blank::before {
    left: 18px;
    right: 18px;
    color: rgba(117, 100, 88, 0.78);
    font-style: normal;
}

.diary-meta-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.diary-downloads {
    margin-top: 18px;
}

.diary-help {
    margin-top: 24px;
}

.diary-help ul {
    margin: 12px 0 0;
    padding-left: 18px;
}

.newapp-is-loading,
button.newapp-is-loading,
.diary-btn.newapp-is-loading,
.rephrase-btn.newapp-is-loading {
    cursor: wait;
    opacity: 0.82;
    pointer-events: none;
}

.newapp-loader {
    display: inline-flex;
    width: 16px;
    height: 16px;
    margin-right: 7px;
    border: 2px solid rgba(255, 255, 255, 0.45);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: newapp-loader-spin 0.75s linear infinite;
    vertical-align: -3px;
}

.newapp-section-loading {
    position: relative;
}

.newapp-section-loading::after {
    content: 'Loading...';
    position: absolute;
    inset: 12px 12px auto auto;
    z-index: 5;
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 7px 12px;
    border-radius: 999px;
    background: rgba(45, 34, 27, 0.88);
    color: #fff;
    font-size: 0.78rem;
    font-weight: 700;
    box-shadow: var(--shadow-md);
}

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

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

#utilities-tab {
    display: flex !important;
    flex-direction: column;
    min-height: calc(100vh - 170px);
}

@media (max-width: 1240px) {
    .main-wrap {
        flex-direction: column;
    }

    .button-section {
        width: 100%;
        flex-basis: auto;
        max-height: none;
        overflow: visible;
    }

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

    .input-controls {
        flex-direction: column;
        align-items: stretch;
    }

    .chain-toggle {
        justify-content: space-between;
    }

    .copy-btn {
        position: static;
        justify-self: end;
    }
}

@media (max-width: 720px) {
    body {
        padding: 10px;
    }

    header {
        padding: 28px 18px 24px;
    }

    .page-nav {
        justify-content: flex-start;
    }

    .stats-container,
    .main-wrap {
        padding-left: 16px;
        padding-right: 16px;
    }

    .find-replace {
        grid-template-columns: 1fr;
    }

    textarea {
        min-height: 220px;
    }
}

@media (max-width: 920px) {
    .diary-layout {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 640px) {
    .diary-shell {
        padding: 18px;
    }

    .diary-sidebar,
    .diary-editor-card,
    .diary-help {
        padding: 18px;
    }

    .diary-actions,
    .diary-downloads {
        flex-direction: column;
    }

    .diary-btn {
        width: 100%;
    }
}
