* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Segoe UI', 'Noto Sans JP', 'Noto Sans KR', 'Noto Sans SC', 'Noto Sans TC', 'Noto Sans Thai', sans-serif;
    background: #f0f2f5;
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
}

.header-row {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

h1 {
    text-align: center;
    font-size: 2em;
    font-weight: 700;
    color: #1a1a2e;
    margin: 0;
}
.logo-link {
    text-decoration: none;
    color: inherit;
    cursor: pointer;
}
.logo-link:hover {
    opacity: 0.7;
}
h1 .subtitle {
    font-weight: 300;
    font-size: 0.5em;
    color: #666;
    display: block;
}

.ui-lang-picker {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin-bottom: 8px;
}
.ui-lang-picker label {
    font-size: 12px;
    color: #666;
    white-space: nowrap;
}
.ui-lang-picker select {
    width: auto;
    padding: 4px 8px;
    font-size: 13px;
    border-radius: 6px;
}

.btn-inline {
    margin-left: 6px;
    vertical-align: middle;
    padding: 2px 8px;
    font-size: 11px;
}

/* Controls */
.controls {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.control-row {
    display: flex;
    gap: 15px;
    align-items: flex-end;
    margin-bottom: 15px;
}
.control-row:last-child { margin-bottom: 0; }

.control-group {
    flex: 1;
}
.control-group label {
    display: block;
    font-weight: 600;
    font-size: 13px;
    color: #555;
    margin-bottom: 6px;
}
.hint {
    font-weight: 400;
    color: #999;
    font-size: 11px;
}

select {
    width: 100%;
    padding: 8px 12px;
    border: 2px solid #e0e0e0;
    border-radius: 6px;
    font-size: 14px;
    background: #fff;
    cursor: pointer;
}
select:focus { border-color: #4a90d9; outline: none; }

.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    background: #e8ecf1;
    color: #333;
    transition: all 0.15s;
    white-space: nowrap;
}
.btn:hover { background: #d0d5dd; }
.btn-accent { background: #4a90d9; color: #fff; }
.btn-accent:hover { background: #3a7bc8; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-group { display: flex; gap: 6px; }

/* Language summary (inline pills + open modal button) */
.lang-summary {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    align-items: center;
}
.lang-pill {
    display: inline;
    font-size: 12px;
    color: #555;
    white-space: nowrap;
}
.lang-pill.experimental { }
.lang-pill + .lang-pill::before {
    content: '/ ';
    color: #bbb;
}
.btn-open-modal {
    padding: 3px 10px;
    border-radius: 12px;
    background: #fff;
    border: 1.5px solid #ccc;
    font-size: 11px;
    color: #666;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s;
}
.btn-open-modal:hover { background: #f0f2f5; color: #333; }
.exp-badge {
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    background: #e67e22;
    border-radius: 3px;
    padding: 2px 5px;
    margin-left: 4px;
    vertical-align: middle;
    letter-spacing: 0.5px;
}
.hist-badge {
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    background: #8e44ad;
    border-radius: 3px;
    padding: 2px 5px;
    margin-left: 4px;
    vertical-align: middle;
}
.lang-label .hist-badge,
.lang-label .exp-badge {
    font-size: 9px;
    color: #999;
    background: none;
    border: 1px solid #ccc;
    padding: 1px 3px;
}

/* Language selector modal */
.lang-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 2000;
    justify-content: center;
    align-items: center;
}
.lang-modal-overlay.open { display: flex; }
.lang-modal {
    background: #fff;
    border-radius: 14px;
    width: 90vw;
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.lang-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px 12px;
    border-bottom: 1px solid #eee;
}
.lang-modal-title {
    font-size: 16px;
    font-weight: 700;
    color: #1a1a2e;
}
.lang-modal-actions-top { display: flex; gap: 6px; }
.lang-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 12px 20px;
    -webkit-overflow-scrolling: touch;
}
.lang-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px 16px;
    border-top: 1px solid #eee;
}

/* Groups inside modal */
.lang-group {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    width: 100%;
    padding: 6px 0;
    border-bottom: 1px solid #f0f0f0;
}
.lang-group:last-child { border-bottom: none; }
.lang-group-label {
    flex-shrink: 0;
    width: 72px;
    font-size: 0.7rem;
    font-weight: 600;
    color: #888;
    padding-top: 5px;
    line-height: 1.2;
}
.lang-group-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    flex: 1;
}

/* Toggle pills inside modal */
.lang-toggle {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 20px;
    background: #f0f2f5;
    font-size: 12px;
    cursor: pointer;
    user-select: none;
    transition: all 0.15s;
    border: 2px solid transparent;
}
.lang-toggle.active {
    background: #e3f0ff;
    border-color: #4a90d9;
}
.lang-toggle input { display: none; }
.lang-toggle .dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: #ccc;
    transition: background 0.15s;
}
.lang-toggle.active .dot { background: #4a90d9; }
.lang-toggle.experimental { }
.lang-toggle.experimental.active { }


/* RTL note */
.rtl-note {
    text-align: center;
    padding: 8px 16px;
    margin-bottom: 10px;
    font-size: 12px;
    color: #888;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
}
.rtl-switch {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 12px;
    color: #555;
    font-weight: 600;
    white-space: nowrap;
}
.rtl-switch input { display: none; }
.rtl-switch-slider {
    position: relative;
    width: 34px;
    height: 18px;
    background: #ccc;
    border-radius: 9px;
    transition: background 0.2s;
    flex-shrink: 0;
}
.rtl-switch-slider::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s;
}
.rtl-switch input:checked + .rtl-switch-slider {
    background: #4a90d9;
}
.rtl-switch input:checked + .rtl-switch-slider::after {
    transform: translateX(16px);
}

/* RTL native direction mode */
.lang-row.rtl-native .lang-text {
    direction: rtl;
}

/* Empty message */
.empty-message {
    text-align: center;
    padding: 40px 20px;
    color: #999;
    font-size: 15px;
}

/* Map container */
.map-container {
    position: relative;
    background: #fff;
    border-radius: 12px;
    padding: 30px 40px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    overflow: visible;
    display: inline-block;
    min-width: 320px;
}

.map-toolbar {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 8px;
}

/* Wrapper to center the inline-block map container */
.map-wrapper {
    text-align: center;
}

.lines-svg {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 1;
}

#langRows {
    position: relative;
    z-index: 2;
    display: inline-block;
    min-width: 100%;
}

/* Language row */
.lang-row {
    display: flex;
    align-items: baseline;
    margin-bottom: 60px;
    flex-wrap: nowrap;
    white-space: nowrap;
    position: relative;
    cursor: grab;
    transition: opacity 0.15s, background 0.15s;
    border-radius: 6px;
    padding: 4px 8px;
    margin-left: -8px;
    margin-right: -8px;
}
.lang-row:last-child { margin-bottom: 0; }
.lang-row.dragging { opacity: 0.7; z-index: 1000; }

.lang-label {
    font-weight: 700;
    font-size: 16px;
    color: #333;
    min-width: 160px;
    width: 240px;
    margin-right: 20px;
    flex-shrink: 0;
    cursor: grab;
    touch-action: none;
    user-select: none;
    text-align: left;
}

.lang-row.dragging .lang-label {
    cursor: grabbing;
}

.lang-text {
    display: flex;
    align-items: baseline;
    gap: 0;
    flex-wrap: nowrap;
}
/* RTL languages: segments flow left-to-right like all languages (for word-order comparison),
   but text within each segment renders right-to-left */
.lang-row[data-lang="ar"] .segment,
.lang-row[data-lang="ar_eg"] .segment,
.lang-row[data-lang="he"] .segment,
.lang-row[data-lang="ar"] .segment-input,
.lang-row[data-lang="ar_eg"] .segment-input,
.lang-row[data-lang="he"] .segment-input {
    direction: rtl;
    unicode-bidi: isolate;
}

.segment {
    font-size: 20px;
    font-weight: 600;
    padding: 2px 4px;
    border-radius: 3px;
    position: relative;
    cursor: default;
    transition: all 0.15s;
    white-space: nowrap;
}
.segment:hover {
    transform: scale(1.05);
    z-index: 10;
}
.segment-dual {
    display: inline-flex;
    gap: 6px;
    align-items: flex-end;
}
.hiero-pair {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
}
.segment-hiero {
    font-family: 'Noto Sans Egyptian Hieroglyphs', sans-serif;
    font-size: 24px;
    font-weight: 700;
}
.segment-translit {
    font-size: 14px;
    font-weight: 400;
    opacity: 0.65;
}
.segment-space {
    width: 6px;
    display: inline-block;
}
.segment-punct {
    font-size: 20px;
    font-weight: 600;
    color: #888;
    padding: 2px 0 2px 1px;
}
.segment-compound {
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* Copy & Edit buttons */
.copy-btn, .edit-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    width: 28px;
    height: 28px;
    font-size: 14px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    color: #666;
}
.edit-btn { right: 4px; }
.copy-btn { right: 36px; }
.lang-row:hover .edit-btn,
.lang-row:hover .copy-btn {
    opacity: 1;
}
.edit-btn:hover, .copy-btn:hover {
    background: #e3f0ff;
    border-color: #4a90d9;
    color: #4a90d9;
}

/* Editing overlay — covers entire page */
.edit-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: linear-gradient(135deg, rgba(30,30,50,0.4) 0%, rgba(60,60,90,0.3) 100%);
    backdrop-filter: blur(2px);
    z-index: 50;
    animation: fadeInOverlay 0.2s ease-out;
}
@keyframes fadeInOverlay {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Editing mode */
.lang-row.editing {
    position: fixed;
    z-index: 51;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: linear-gradient(135deg, #ffffff 0%, #f8faff 100%);
    flex-wrap: wrap;
    white-space: normal;
    cursor: default;
    padding: 24px 32px 16px;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: 80vh;
    border-radius: 14px;
    box-shadow: 0 8px 40px rgba(74, 144, 217, 0.22), 0 0 0 1px rgba(74, 144, 217, 0.12);
    width: 85vw;
    max-width: 1200px;
    box-sizing: border-box;
    max-height: 85vh;
    overflow-y: auto;
}
.lang-row.editing:active {
    cursor: default;
}
.lang-row.editing .edit-btn,
.lang-row.editing .copy-btn {
    display: none;
}
.lang-row.editing .lang-label {
    width: 100%;
    font-size: 18px;
    margin-bottom: 12px;
    text-align: center;
}
.lang-row.editing .lang-text {
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    width: 100%;
}

/* Segment wrapper — vertical layout with handle on top, delete on bottom */
.segment-input-wrapper {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    border-radius: 8px;
    padding: 4px 2px;
    transition: background 0.15s;
    background: rgba(0,0,0,0.02);
    border: 1px solid transparent;
}
.segment-input-wrapper:hover {
    background: rgba(74, 144, 217, 0.06);
    border-color: rgba(74, 144, 217, 0.15);
}
.segment-input-wrapper.dragging { opacity: 0.4; }
.segment-input-wrapper.drag-over-left {
    box-shadow: -3px 0 0 0 #4a90d9;
}
.segment-input-wrapper.drag-over-right {
    box-shadow: 3px 0 0 0 #4a90d9;
}

/* Segment ID badge */
.seg-id-badge {
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    border-radius: 3px;
    padding: 1px 5px;
    line-height: 1.3;
    user-select: none;
}

/* Drag handle — top of wrapper */
.drag-handle {
    font-size: 16px;
    color: #bbb;
    cursor: grab;
    padding: 2px 4px;
    user-select: none;
    line-height: 1;
    transition: color 0.15s;
}
.segment-input-wrapper:hover .drag-handle {
    color: #4a90d9;
}
.drag-handle:active { cursor: grabbing; }

/* Delete button — bottom of wrapper */
.seg-delete-btn {
    font-size: 16px;
    color: #ccc;
    cursor: pointer;
    padding: 2px 4px;
    line-height: 1;
    user-select: none;
    transition: color 0.15s;
}
.seg-delete-btn:hover {
    color: #e74c3c;
}

.add-segment-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 2px dashed #ccc;
    border-radius: 8px;
    background: transparent;
    color: #999;
    font-size: 20px;
    cursor: pointer;
    margin-left: 4px;
    transition: all 0.15s;
    flex-shrink: 0;
    align-self: center;
}
.add-segment-btn:hover {
    border-color: #4a90d9;
    color: #4a90d9;
    background: rgba(74, 144, 217, 0.05);
}

.add-segment-menu {
    position: absolute;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.12);
    padding: 4px 0;
    z-index: 100;
    max-height: 200px;
    overflow-y: auto;
}
.add-segment-menu-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
}
.add-segment-menu-item:hover {
    background: #f0f2f5;
}
.add-segment-menu-item .seg-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.segment-input {
    font-size: 22px;
    font-weight: 600;
    padding: 6px 8px;
    border: 1px dashed;
    border-radius: 6px;
    background: #fff;
    outline: none;
    font-family: inherit;
    min-width: 40px;
    text-align: center;
}
.segment-input:focus {
    box-shadow: 0 0 0 3px rgba(74, 144, 217, 0.25);
    border-style: solid;
}

.save-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    width: 100%;
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid rgba(74, 144, 217, 0.15);
}
.save-bar .btn {
    padding: 10px 24px;
    font-size: 14px;
}

.cancel-btn {
    background: #f0f0f0;
    color: #666;
}

/* Clickable seg-id-badge in edit mode */
.segment-input-wrapper .seg-id-badge {
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s;
}
.segment-input-wrapper .seg-id-badge:hover {
    transform: scale(1.15);
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

/* Active item in segment type menu */
.add-segment-menu-item.active {
    background: #e8f0fe;
    font-weight: 600;
}

/* Touch dragging */
.touch-dragging {
    opacity: 0.85;
    box-shadow: 0 8px 24px rgba(0,0,0,0.2);
    border-radius: 8px;
    background: #fff;
}
.touch-placeholder {
    border: 2px dashed #4a90d9;
    border-radius: 8px;
    background: rgba(74, 144, 217, 0.05);
    min-height: 40px;
}

/* Footer */
.footer {
    margin-top: 15px;
    text-align: center;
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}
.footer-sep {
    width: 1px;
    height: 20px;
    background: #ccc;
    margin: 0 4px;
}
.btn-share { font-weight: 700; }
.btn-x { background: #000; color: #fff; }
.btn-x:hover { background: #333; }
.btn-fb { background: #1877f2; color: #fff; }
.btn-fb:hover { background: #1565c0; }
.btn-line { background: #06c755; color: #fff; }
.btn-line:hover { background: #05a847; }
.site-footer {
    margin-top: 20px;
    padding: 12px 0;
    text-align: center;
    font-size: 0.82rem;
    color: #888;
    border-top: 1px solid #ddd;
}
.site-footer a {
    color: #666;
    text-decoration: none;
}
.site-footer a:hover {
    color: #333;
    text-decoration: underline;
}
.footer-dot {
    margin: 0 6px;
    color: #ccc;
}
.site-footer-links {
    margin-bottom: 8px;
}
.disclaimer {
    font-size: 0.75rem;
    color: #aaa;
    margin: 8px 0 0;
    line-height: 1.6;
}
.disclaimer a {
    color: #999;
}
.disclaimer a:hover {
    color: #333;
}
.copy-toast {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: #fff;
    padding: 8px 20px;
    border-radius: 20px;
    font-size: 13px;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
    z-index: 1000;
}
.copy-toast.show { opacity: 1; }

/* Responsive */
@media (max-width: 768px) {
    body { padding: 10px; overflow-x: hidden; }
    .container { max-width: 100vw; }
    .control-row { flex-direction: column; align-items: stretch; }
    .map-container { padding: 15px 10px; min-width: 0; }
    .lang-label { min-width: 80px; width: 80px; font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex-shrink: 0; margin-right: 8px; }
    .segment { font-size: 16px; }
    .lang-row { margin-bottom: 40px; flex-wrap: nowrap; white-space: nowrap; }
    /* Export mode: override mobile constraints for full-width export */
    .export-mode .lang-label { min-width: 160px; width: 240px; font-size: 16px; overflow: visible; text-overflow: unset; margin-right: 20px; }
    .export-mode .segment { font-size: 20px; }
    .export-mode .lang-row { margin-bottom: 60px; }
    .export-mode { padding: 30px 40px; }

    /* Compact language summary pills for mobile */
    .lang-summary { gap: 3px; }
    .lang-pill { font-size: 10px; }
    .btn-open-modal { font-size: 10px; padding: 2px 8px; }

    /* Compact modal for mobile */
    .lang-modal {
        width: 95vw;
        max-height: 85vh;
        border-radius: 10px;
    }
    .lang-modal-header { padding: 12px 14px 10px; }
    .lang-modal-body { padding: 8px 14px; }
    .lang-modal-footer { padding: 10px 14px 14px; }
    .lang-modal-title { font-size: 14px; }
    .lang-toggle {
        font-size: 11px;
        padding: 3px 7px;
        gap: 3px;
        border-width: 1.5px;
    }
    .lang-toggle .dot { width: 6px; height: 6px; }
    .lang-group { gap: 4px; padding: 4px 0; }
    .lang-group-label { width: 52px; font-size: 0.6rem; padding-top: 4px; }
    .lang-group-toggles { gap: 3px; }

    /* Horizontal scroll for long language rows */
    .map-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Mobile editing improvements */
    .lang-row.editing {
        width: 95vw;
        padding: 16px 12px 12px;
        max-height: 90vh;
    }
    .lang-row.editing .lang-label {
        font-size: 15px;
        margin-bottom: 8px;
    }
    .lang-row.editing .lang-text {
        gap: 6px;
    }
    .segment-input {
        font-size: 16px;
        padding: 5px 6px;
        min-width: 36px;
    }
    .segment-input-wrapper {
        padding: 3px 1px;
    }
    .drag-handle {
        font-size: 18px;
        padding: 4px 8px;
    }
    .seg-delete-btn {
        font-size: 18px;
        padding: 4px 8px;
    }
    .seg-id-badge {
        font-size: 9px;
        padding: 2px 4px;
    }
    .save-bar {
        margin-top: 12px;
        padding-top: 10px;
    }
    .save-bar .btn {
        padding: 8px 20px;
        font-size: 13px;
    }
    .add-segment-btn {
        width: 32px;
        height: 32px;
        font-size: 18px;
    }
    .add-segment-menu {
        max-height: 50vh;
    }

    /* Edit button always visible on mobile */
    .edit-btn {
        opacity: 1 !important;
    }
}
