:root {
    --bg: #f3f6f2;
    --panel: #fcfdfb;
    --border: #d2d9cf;
    --ink: #1e2b20;
    --muted: #5e6f60;
    --brand: #1f6b43;
    --brand-soft: #dcebdd;
    --warn: #7a4500;
    --error: #8f1f1f;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: radial-gradient(circle at top right, #e8efe6 0%, var(--bg) 45%, #eef2ea 100%);
    color: var(--ink);
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.tw-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    padding: 18px 22px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(135deg, #f8fbf7, #eef5ee);
}

.tw-header h1 {
    margin: 0 0 6px;
    font-family: Georgia, "Times New Roman", serif;
    font-size: 1.6rem;
}

.tw-header p {
    margin: 0;
    color: var(--muted);
}

.tw-header-actions {
    display: flex;
    gap: 10px;
}

.tw-status-strip {
    position: sticky;
    top: 0;
    z-index: 36;
    padding: 8px 14px 0;
}

.tw-status-output {
    background: #f8fbf7;
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 8px 10px;
}

.tw-layout {
    display: grid;
    grid-template-columns: 68px 1fr;
    gap: 14px;
    padding: 14px;
    min-height: calc(100vh - 148px);
}

body.tw-sidebar-open .tw-layout,
body.tw-sidebar-pinned .tw-layout {
    grid-template-columns: minmax(280px, 360px) 1fr;
}

.tw-sidebar,
.tw-workspace {
    min-height: 0;
}

.tw-panel,
.tw-workspace,
.tw-pane,
.tw-validation {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
}

.tw-panel {
    padding: 12px;
    margin-bottom: 12px;
}

.tw-sidebar-panel-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.tw-sidebar-panel-head h2 {
    margin: 0;
    font-size: 1rem;
}

.tw-sidebar-panel-body {
    display: block;
}

body:not(.tw-sidebar-open):not(.tw-sidebar-pinned) .tw-sidebar .tw-sidebar-panel-body,
body:not(.tw-sidebar-open):not(.tw-sidebar-pinned) .tw-sidebar .tw-sidebar-panel-head h2 {
    display: none;
}

.tw-panel label {
    display: block;
    margin: 8px 0 4px;
    font-size: 0.85rem;
    color: var(--muted);
}

.tw-panel select,
.tw-panel input,
.tw-panel textarea,
.tw-target-input,
.tw-target-paragraph {
    width: 100%;
    border: 1px solid #c4cec0;
    border-radius: 8px;
    padding: 9px 10px;
    font-size: 0.92rem;
    background: #ffffff;
}

.tw-field-note {
    margin-top: 6px;
    font-size: 0.78rem;
    color: var(--muted);
    line-height: 1.35;
}

.tw-entry-count {
    margin-top: 8px;
    font-size: 0.82rem;
    color: var(--muted);
}

.tw-entry-list {
    margin-top: 8px;
    border-top: 1px solid var(--border);
    max-height: 44vh;
    overflow: auto;
}

body:not(.tw-sidebar-open):not(.tw-sidebar-pinned) .tw-sidebar {
    overflow: hidden;
}

body:not(.tw-sidebar-open):not(.tw-sidebar-pinned) .tw-sidebar .tw-panel {
    padding: 8px;
}

.tw-entry-item {
    width: 100%;
    text-align: left;
    padding: 10px 8px;
    border: 0;
    border-bottom: 1px solid #edf1eb;
    background: transparent;
    cursor: pointer;
}

.tw-entry-item:hover,
.tw-entry-item.active {
    background: #edf5ed;
}

.tw-entry-item .k {
    display: inline-block;
    min-width: 56px;
    color: var(--brand);
    font-weight: 600;
}

.tw-entry-item .t {
    color: var(--ink);
}

.tw-workspace {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 12px;
    min-height: calc(100vh - 172px);
}

.tw-toolbar {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.tw-meta {
    color: var(--muted);
    font-size: 0.9rem;
}

.tw-toolbar-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.tw-btn {
    background: var(--brand);
    color: #fff;
    border: 1px solid #145538;
    border-radius: 8px;
    padding: 8px 12px;
    cursor: pointer;
    text-decoration: none;
    font-size: 0.9rem;
}

.tw-btn:hover {
    filter: brightness(0.95);
}

.tw-btn-secondary {
    background: #f8faf7;
    color: var(--ink);
    border-color: #bccab9;
}

.tw-main-panes {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    height: calc(100vh - 276px);
    min-height: 420px;
}

.tw-pane {
    padding: 10px;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.tw-pane h2 {
    margin: 0 0 8px;
    font-size: 1rem;
}

.tw-source-view,
.tw-target-view {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border-top: 1px solid var(--border);
    padding-top: 8px;
}

.tw-source-section,
.tw-target-section {
    border: 1px solid #e0e7dc;
    border-radius: 8px;
    padding: 8px;
    margin-bottom: 8px;
    background: #ffffff;
}

.tw-target-section-source {
    border-left: 4px solid #4d7d61;
}

.tw-target-section-added {
    border-left: 4px solid #8b5c2a;
    background: #fff9f1;
}

.tw-kind-chip {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.72rem;
    background: #edf2ec;
    color: #3f5d49;
}

.tw-kind-chip-added {
    background: #f8e9d8;
    color: #7a4d11;
}

.tw-add-block-controls {
    border: 1px dashed #c9d6c7;
    border-radius: 8px;
    padding: 8px;
    margin-top: 8px;
    background: #f9fcf8;
}

.tw-add-block-controls label {
    display: block;
    margin: 6px 0 4px;
    font-size: 0.8rem;
    color: var(--muted);
}

.tw-add-block-controls textarea,
.tw-add-block-controls select {
    width: 100%;
    border: 1px solid #c4cec0;
    border-radius: 8px;
    padding: 8px;
    font-size: 0.88rem;
    background: #fff;
}

.tw-target-only-type {
    border: 1px solid #d8b794;
    background: #fff;
    color: #6e4615;
    border-radius: 7px;
    padding: 4px 8px;
    font-size: 0.82rem;
}

.tw-target-only-text {
    min-height: 92px;
}

.tw-source-block-controls,
.tw-source-para-actions {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 6px;
}

.tw-block-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 6px;
}

.tw-mini-btn {
    border: 1px solid #bccab9;
    border-radius: 7px;
    background: #f7faf6;
    color: #274937;
    font-size: 0.8rem;
    padding: 4px 8px;
    cursor: pointer;
}

.tw-mini-btn-danger {
    border-color: #d7a8a8;
    color: #8f1f1f;
    background: #fff4f4;
}

.tw-section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.tw-jump-btn {
    border: 1px solid #c7d7c6;
    border-radius: 999px;
    padding: 3px 9px;
    font-size: 0.75rem;
    background: #f8fbf8;
    color: #2d5c3e;
    cursor: pointer;
}

.tw-jump-btn:hover {
    background: #ecf5ec;
}

.tw-source-section h4,
.tw-target-section h4 {
    margin: 0 0 6px;
    font-size: 0.92rem;
}

.tw-source-section p,
.tw-target-section p {
    margin: 0 0 7px;
    font-size: 0.9rem;
    line-height: 1.4;
}

.tw-source-thumbs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 6px 0 10px;
}

.tw-source-thumb {
    width: 96px;
    height: 96px;
    object-fit: cover;
    border: 1px solid #d8e2dc;
    border-radius: 8px;
    background: #f6f9f5;
}

/* ── Reference section chip layout (mirrors browser.css) ─────────────────── */
.tw-source-section.tw-refs-section,
.tw-target-section.tw-refs-section {
    background: #f7fbff;
    border-left-color: #4d90c6;
}

.reference-groups {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    align-items: stretch;
}

@media (max-width: 1200px) {
    .reference-groups { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 760px) {
    .reference-groups { grid-template-columns: 1fr; }
}

.reference-group {
    background: #fff;
    border: 1px solid #dde9f5;
    border-radius: 8px;
    padding: 8px;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.reference-group-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 6px;
    margin-bottom: 6px;
}

.reference-group-title { flex: 1; }

.reference-sense {
    font-size: 0.96rem;
    font-weight: 700;
    color: #1f3550;
    margin-bottom: 2px;
    line-height: 1.2;
}

.reference-rendering {
    font-size: 0.86rem;
    color: #4f6479;
    margin-bottom: 0;
    line-height: 1.2;
}

.reference-list {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 5px;
    margin-top: 6px;
}

.reference-list.collapsed { display: none; }

.reference-toggle-btn {
    background: none;
    border: none;
    color: #4d90c6;
    font-size: 1rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    font-family: inherit;
    flex-shrink: 0;
    transition: transform 0.2s ease;
}

.reference-toggle-btn.collapsed { transform: rotate(-90deg); }
.reference-toggle-btn:hover { color: #2c5aa0; }

.reference-chip {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    max-width: 100%;
    padding: 3px 7px;
    border-radius: 999px;
    border: 1px solid #cfe0f1;
    background: #f4f9ff;
    color: #26445f;
    font-size: 0.77rem;
    font-weight: 600;
    font-family: inherit;
    white-space: normal;
    line-height: 1.2;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.reference-note {
    padding: 10px;
    border-radius: 8px;
    background: #ffffff;
    border: 1px dashed #cdd9e5;
    color: #4d6072;
    font-size: 0.9rem;
}

.tw-source-image-missing {
    width: 96px;
    min-height: 96px;
    border: 1px dashed #d8b7b7;
    border-radius: 8px;
    background: #fff4f4;
    color: #8d3a3a;
    font-size: 0.68rem;
    line-height: 1.2;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.tw-lock {
    display: inline-block;
    margin: 0 0 6px;
    padding: 2px 7px;
    border-radius: 999px;
    font-size: 0.75rem;
    background: #ecefec;
    color: #43604b;
}

.tw-validation {
    padding: 10px;
}

.tw-validation h3 {
    margin: 0 0 8px;
}

.tw-msg-ok,
.tw-msg-warn,
.tw-msg-error {
    padding: 7px 9px;
    border-radius: 7px;
    margin-bottom: 4px;
    font-size: 0.88rem;
}

.tw-msg-ok {
    background: #e8f4ea;
    color: #1d5b34;
}

.tw-msg-warn {
    background: #fff0de;
    color: var(--warn);
}

.tw-msg-error {
    background: #ffe3e3;
    color: var(--error);
}

.tw-section-focus {
    outline: 2px solid #8bc49b;
    box-shadow: 0 0 0 3px #e4f4e8;
}

body.tw-reader-preview .tw-jump-btn,
body.tw-reader-preview .tw-add-block-controls,
body.tw-reader-preview .tw-block-actions,
body.tw-reader-preview .tw-kind-chip,
body.tw-reader-preview .tw-lock,
body.tw-reader-preview .tw-target-input,
body.tw-reader-preview .tw-target-paragraph,
body.tw-reader-preview .tw-target-only-text {
    display: none;
}

.tw-preview-edit-btn {
    margin-left: auto;
}

.tw-preview-inline-row {
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.tw-preview-inline-row p {
    flex: 1;
}

.tw-preview-inline-input {
    width: 100%;
    border: 1px solid #c4cec0;
    border-radius: 8px;
    padding: 9px 10px;
    font-size: 0.92rem;
    background: #ffffff;
}

.tw-preview-inline-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 6px;
}

body.tw-reader-preview .tw-target-section {
    background: #fff;
}

.tw-settings-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(20, 28, 22, 0.38);
    opacity: 0;
    pointer-events: none;
    transition: opacity 160ms ease;
    z-index: 40;
}

.tw-settings-panel {
    position: fixed;
    top: 0;
    right: -460px;
    width: min(460px, 94vw);
    height: 100vh;
    background: #f8fbf7;
    border-left: 1px solid var(--border);
    box-shadow: -12px 0 34px rgba(0, 0, 0, 0.12);
    padding: 16px;
    overflow: auto;
    transition: right 180ms ease;
    z-index: 41;
}

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

.tw-settings-head h2 {
    margin: 0;
    font-size: 1.1rem;
}

body.tw-settings-open .tw-settings-backdrop {
    opacity: 1;
    pointer-events: auto;
}

body.tw-settings-open .tw-settings-panel {
    right: 0;
}

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

    .tw-main-panes {
        grid-template-columns: 1fr;
        height: auto;
        min-height: 0;
    }

    .tw-source-view,
    .tw-target-view {
        max-height: 48vh;
    }

    .tw-sidebar {
        position: fixed;
        top: 96px;
        left: 0;
        width: min(340px, 88vw);
        height: calc(100vh - 96px);
        z-index: 45;
        transform: translateX(-102%);
        transition: transform 180ms ease;
        box-shadow: 8px 0 22px rgba(0, 0, 0, 0.14);
    }

    body.tw-sidebar-open .tw-sidebar,
    body.tw-sidebar-pinned .tw-sidebar {
        transform: translateX(0);
    }
}
