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

:root {
    --bg: #0e0e14;
    --card: #181820;
    --card-border: rgba(255, 255, 255, 0.07);
    --text: #e8e8f0;
    --text-muted: #6b6b80;
    --text-dim: #48485a;
    --primary: #0d9488;
    --primary-light: #5eead4;
    --green: #10b981;
    --green-bg: rgba(16, 185, 129, 0.1);
    --red: #ef4444;
    --red-bg: rgba(239, 68, 68, 0.1);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-full: 9999px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    line-height: 1.5;
    overflow-x: hidden;
    display: flex;
    justify-content: center;
}

[x-cloak] { display: none !important; }

/* ─── App Shell ─── */

.app {
    width: 100%;
    max-width: 480px;
    padding: 20px 16px 32px;
    position: relative;
}

/* ─── Header ─── */

.header {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 28px;
    padding-top: 8px;
}

.header-icon {
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    border-radius: var(--radius-md);
    color: #fff;
    flex-shrink: 0;
}

.header-title {
    font-size: 1.5rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    line-height: 1.2;
    color: #fff;
}

.header-accent {
    color: var(--primary-light);
}

.header-sub {
    font-size: 0.78rem;
    color: var(--text-muted);
    font-weight: 500;
    margin-top: 1px;
}

.header-sub a {
    color: var(--primary-light);
    text-decoration: none;
    font-weight: 600;
}

.header-sub a:hover {
    text-decoration: underline;
}

/* ─── Segmented Control ─── */

.segmented {
    display: flex;
    gap: 4px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-md);
    padding: 3px;
    margin-bottom: 20px;
}

.seg-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 16px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
    -webkit-user-select: none;
    min-height: 42px;
}

.seg-btn:hover { color: var(--text-muted); }

.seg-btn.active {
    background: var(--primary);
    color: #fff;
}

.seg-icon {
    width: 16px;
    height: 16px;
}

/* ─── Cards ─── */

.card {
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 16px;
}

.card-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 16px;
}

.card-label-icon {
    width: 14px;
    height: 14px;
}

/* ─── Upload Zone ─── */

.upload-zone {
    border: 2px dashed rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    padding: 28px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: rgba(0, 0, 0, 0.15);
}

.upload-zone:hover,
.upload-zone:active {
    border-color: var(--primary);
    background: rgba(13, 148, 136, 0.04);
}

.upload-label {
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.upload-icon {
    width: 32px;
    height: 32px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.upload-text {
    color: var(--text);
    font-weight: 600;
    font-size: 0.88rem;
}

.upload-hint {
    color: var(--text-dim);
    font-size: 0.75rem;
}

.upload-file {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 12px;
    padding: 8px 14px;
    background: var(--green-bg);
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--green);
}

.upload-file-icon {
    width: 16px;
    height: 16px;
}

.upload-clear {
    background: none;
    border: none;
    color: var(--green);
    cursor: pointer;
    padding: 4px;
    display: flex;
    opacity: 0.6;
    transition: opacity 0.15s;
}

.upload-clear:hover { opacity: 1; }

.upload-clear svg {
    width: 16px;
    height: 16px;
}

/* ─── Textarea ─── */

textarea {
    width: 100%;
    padding: 14px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-md);
    color: var(--text);
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
    font-size: 0.78rem;
    line-height: 1.6;
    resize: vertical;
    min-height: 120px;
    outline: none;
    margin-top: 14px;
    transition: border-color 0.2s;
}

textarea:focus {
    border-color: var(--primary);
}

textarea::placeholder {
    color: var(--text-dim);
}

/* ─── Source Toggles ─── */

.source-group {
    margin-bottom: 16px;
}

.source-label {
    display: block;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 8px;
}

.source-toggles {
    display: flex;
    gap: 8px;
}

.source-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    background: rgba(0, 0, 0, 0.15);
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
    -webkit-user-select: none;
    min-height: 46px;
}

.source-btn:hover {
    border-color: rgba(255, 255, 255, 0.12);
    color: var(--text-muted);
}

.source-btn.on {
    background: rgba(13, 148, 136, 0.12);
    border-color: var(--primary);
    color: var(--primary-light);
}

.source-btn svg {
    width: 16px;
    height: 16px;
}

/* ─── Search Section ─── */

.search-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 4px;
}

.search-box {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    padding: 0 14px;
    transition: border-color 0.2s;
}

.search-box:focus-within {
    border-color: var(--primary);
}

.search-box-icon {
    width: 18px;
    height: 18px;
    color: var(--text-dim);
    flex-shrink: 0;
}

.search-box input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text);
    font-family: var(--font);
    font-size: 0.88rem;
    font-weight: 500;
    padding: 14px 0;
    outline: none;
    min-width: 0;
}

.search-box input::placeholder {
    color: var(--text-dim);
    font-weight: 400;
}

.search-clear {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    padding: 4px;
    display: flex;
    flex-shrink: 0;
    transition: color 0.15s;
}

.search-clear:hover { color: var(--text-muted); }

.search-clear svg {
    width: 18px;
    height: 18px;
}

.search-controls {
    display: flex;
    gap: 8px;
}

/* ─── Percentage Input ─── */

.pct-group {
    display: flex;
    align-items: center;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: border-color 0.2s;
    flex: 1;
}

.pct-group:focus-within {
    border-color: var(--primary);
}

.pct-input {
    width: 56px;
    padding: 14px 8px;
    border: none;
    background: transparent;
    color: var(--text);
    font-family: var(--font);
    font-size: 0.95rem;
    font-weight: 700;
    text-align: center;
    outline: none;
    -moz-appearance: textfield;
}

.pct-input::-webkit-outer-spin-button,
.pct-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.pct-arrows {
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--card-border);
}

.pct-arrows button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 22px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    padding: 0;
    transition: all 0.1s;
}

.pct-arrows button:hover {
    background: rgba(255, 255, 255, 0.04);
    color: var(--text);
}

.pct-arrows button:active {
    background: rgba(255, 255, 255, 0.08);
}

/* ─── Add Button ─── */

.btn-add {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    min-height: 52px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s;
}

.btn-add:hover { background: #0f766e; }

.btn-add:active {
    background: #115e59;
}

.btn-add svg {
    width: 22px;
    height: 22px;
}

.btn-add:disabled {
    opacity: 0.25;
    cursor: not-allowed;
    background: var(--primary);
}

/* ─── Results Panel ─── */

.results-panel {
    max-height: 200px;
    overflow-y: auto;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    margin-top: 8px;
    margin-bottom: 8px;
    overscroll-behavior: contain;
}

.results-panel::-webkit-scrollbar {
    width: 4px;
}

.results-panel::-webkit-scrollbar-track {
    background: transparent;
}

.results-panel::-webkit-scrollbar-thumb {
    background: var(--text-dim);
    border-radius: 2px;
}

.result-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 12px 14px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
    font-size: 0.78rem;
    cursor: pointer;
    transition: all 0.15s;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.result-item:last-child {
    border-bottom: none;
}

.result-item:hover,
.result-item:focus {
    background: rgba(13, 148, 136, 0.08);
    color: var(--text);
    outline: none;
}

.no-results {
    text-align: center;
    padding: 24px 16px;
    color: var(--text-dim);
    font-size: 0.78rem;
    font-weight: 500;
    letter-spacing: 0.01em;
}

/* ─── Rows ─── */

.rows-list {
    margin-top: 16px;
}

.row-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    margin-bottom: 6px;
    border-radius: var(--radius-sm);
    background: rgba(0, 0, 0, 0.15);
    border: 1px solid var(--card-border);
    transition: border-color 0.2s;
    position: relative;
    overflow: hidden;
}

.row-item::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
}

.row-include::before {
    background: var(--green);
}

.row-remove::before {
    background: var(--red);
}

.row-item:focus-within {
    border-color: var(--green);
}

.row-remove:focus-within {
    border-color: var(--red);
}

.row-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    border: none;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.15s;
    background: var(--green-bg);
    color: var(--green);
}

.row-toggle.remove {
    background: var(--red-bg);
    color: var(--red);
}

.row-toggle:active {
    transform: scale(0.92);
}

.row-toggle-icon {
    width: 18px;
    height: 18px;
}

.row-name {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text);
}

.row-pct-group {
    display: flex;
    align-items: center;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    transition: border-color 0.2s;
    flex-shrink: 0;
}

.row-pct-group:focus-within {
    border-color: var(--green);
}

.row-pct-group.remove:focus-within {
    border-color: var(--red);
}

.row-pct-group.include {
    color: var(--green);
}

.row-pct-group.remove {
    color: var(--red);
}

.row-num {
    width: 44px;
    padding: 8px 4px;
    border: none;
    background: transparent;
    color: inherit;
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 700;
    text-align: center;
    outline: none;
    -moz-appearance: textfield;
}

.row-num::-webkit-outer-spin-button,
.row-num::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.row-pct-sign {
    font-size: 0.7rem;
    font-weight: 600;
    opacity: 0.6;
    padding-right: 2px;
}

.row-pct-arrows {
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--card-border);
}

.row-pct-arrows button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 18px;
    border: none;
    background: transparent;
    color: inherit;
    opacity: 0.5;
    cursor: pointer;
    padding: 0;
    transition: all 0.1s;
}

.row-pct-arrows button:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.05);
}

.row-pct-arrows button:active {
    background: rgba(255, 255, 255, 0.1);
}

.row-remove-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.15s;
}

.row-remove-btn:hover {
    background: var(--red-bg);
    color: var(--red);
}

.row-remove-btn svg {
    width: 15px;
    height: 15px;
}

/* ─── Total Bar ─── */

.total-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 4px 8px;
    margin-top: 4px;
}

.total-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.total-value {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 1rem;
    font-weight: 800;
    transition: color 0.2s;
}

.total-value.valid { color: var(--green); }
.total-value.invalid { color: var(--red); }

.total-check {
    transition: opacity 0.2s, transform 0.2s;
}

.total-value.invalid .total-check {
    opacity: 0;
    transform: scale(0);
}

.total-value.valid .total-check {
    opacity: 1;
    transform: scale(1);
}

/* ─── Progress Track ─── */

.progress-track {
    height: 4px;
    background: rgba(255, 255, 255, 0.06);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.3s, background 0.2s;
}

.fill-valid {
    background: var(--green);
}

.fill-invalid {
    background: var(--red);
}

/* ─── Name Input ─── */

.name-row {
    margin: 16px 0 12px;
}

.name-input-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    padding: 0 14px;
    transition: border-color 0.2s;
}

.name-input-wrap:focus-within {
    border-color: var(--primary);
}

.name-input-icon {
    width: 18px;
    height: 18px;
    color: var(--text-dim);
    flex-shrink: 0;
}

.name-input-wrap input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text);
    font-family: var(--font);
    font-size: 0.88rem;
    font-weight: 500;
    padding: 14px 0;
    outline: none;
    min-width: 0;
}

.name-input-wrap input::placeholder {
    color: var(--text-dim);
    font-weight: 400;
}

/* ─── Create Button ─── */

.btn-create {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    min-height: 52px;
    padding: 14px 24px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: #fff;
    font-family: var(--font);
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s;
    letter-spacing: 0.01em;
}

.btn-create:hover { background: #0f766e; }

.btn-create:active {
    background: #115e59;
}

.btn-create svg {
    width: 20px;
    height: 20px;
}

.btn-create:disabled {
    opacity: 0.25;
    cursor: not-allowed;
    background: var(--primary);
}

/* ─── Result Card ─── */

.result-card {
    margin-top: 16px;
    border: 1px solid rgba(13, 148, 136, 0.2);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: rgba(0, 0, 0, 0.3);
}

.result-card-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--primary-light);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid rgba(13, 148, 136, 0.1);
}

.result-card-icon {
    width: 14px;
    height: 14px;
}

.result-copy-btn {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    border: 1px solid rgba(13, 148, 136, 0.25);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--primary-light);
    font-family: var(--font);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.result-copy-btn:hover {
    background: rgba(13, 148, 136, 0.1);
}

.result-copy-btn:active {
    background: rgba(13, 148, 136, 0.15);
}

.result-data {
    padding: 16px;
    font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
    font-size: 0.82rem;
    line-height: 1.7;
    color: #e0e0e0;
    margin: 0;
    white-space: pre-wrap;
    word-break: break-all;
    background: rgba(0, 0, 0, 0.15);
}

/* ─── Toast ─── */

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-left: 3px solid var(--green);
    color: var(--text);
    padding: 14px 20px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    z-index: 9999;
    white-space: nowrap;
}

.toast-icon {
    width: 20px;
    height: 20px;
    color: var(--green);
    flex-shrink: 0;
}

/* ─── Modal ─── */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    padding: 24px;
}

.modal-box {
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 340px;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 20px 0;
}

.modal-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-warn-icon {
    width: 20px;
    height: 20px;
    color: var(--red);
}

.modal-header h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius-sm);
    display: flex;
    transition: all 0.12s;
}

.modal-close:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.06);
}

.modal-close svg {
    width: 18px;
    height: 18px;
}

.modal-body {
    padding: 12px 20px 20px;
    color: var(--text-muted);
    font-size: 0.88rem;
    line-height: 1.5;
}

.modal-footer {
    display: flex;
    gap: 8px;
    padding: 0 20px 16px;
}

.modal-footer .btn-ghost,
.modal-footer .btn-danger {
    flex: 1;
}

.btn-ghost {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 20px;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    min-height: 46px;
}

.btn-ghost:hover {
    background: rgba(255, 255, 255, 0.04);
    color: var(--text);
}

.btn-danger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--red);
    color: #fff;
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    min-height: 46px;
}

.btn-danger:hover { background: #dc2626; }
.btn-danger:active { background: #b91c1c; }

/* ─── Responsive ─── */

@media (max-width: 380px) {
    .app {
        padding: 16px 12px 24px;
    }

    .card {
        padding: 16px;
    }

    .source-btn {
        font-size: 0.78rem;
        padding: 10px 12px;
        min-height: 42px;
    }

    .search-box input {
        font-size: 0.82rem;
        padding: 12px 0;
    }

    .pct-input {
        width: 48px;
        padding: 12px 6px;
        font-size: 0.88rem;
    }

    .btn-add {
        width: 46px;
        min-height: 46px;
    }

    .row-item {
        padding: 6px 8px;
    }

    .row-num {
        width: 36px;
        font-size: 0.82rem;
        padding: 6px 2px;
    }

    .row-pct-arrows button {
        width: 20px;
        height: 16px;
    }
}

@media (min-width: 640px) {
    .app {
        padding: 32px 24px 40px;
    }

    .header {
        margin-bottom: 36px;
    }

    .header-icon {
        width: 52px;
        height: 52px;
    }

    .header-icon svg {
        width: 32px;
        height: 32px;
    }

    .header-title {
        font-size: 1.75rem;
    }

    .card {
        padding: 24px;
    }

    .result-data {
        font-size: 0.9rem;
        padding: 20px;
    }
}
