:root {
    color-scheme: light;
    --ink: #17212b;
    --muted: #607080;
    --line: #d9e0e6;
    --soft: #f4f7f9;
    --paper: #ffffff;
    --nav: #182936;
    --nav-soft: #263d4c;
    --accent: #087a6b;
    --accent-dark: #046054;
    --accent-soft: #e7f5f2;
    --blue: #246a9a;
    --blue-soft: #eaf3f9;
    --red: #b4233a;
    --red-soft: #fff0f2;
    --amber: #93620a;
    --amber-soft: #fff6df;
    --green: #14734f;
    --green-soft: #e9f7f0;
    --shadow: 0 12px 30px rgba(23, 33, 43, 0.08);
    font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-width: 320px;
    color: var(--ink);
    background: #edf1f4;
}

button, textarea { font: inherit; }
button { cursor: pointer; }

.topbar {
    min-height: 74px;
    padding: 12px clamp(16px, 3vw, 42px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 28px;
    color: #fff;
    background: var(--nav);
    border-bottom: 1px solid #304654;
}

.brand { display: flex; align-items: center; gap: 13px; }
.brand-mark {
    width: 46px;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px solid #78aaa2;
    color: #a9e0d7;
    font: 700 13px/1 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.brand strong { display: block; font-size: 18px; letter-spacing: 0; }
.brand div span { display: block; margin-top: 3px; color: #b8c7d0; font-size: 12px; }

.progress-wrap { width: min(240px, 28vw); color: #d4dee4; font-size: 12px; text-align: right; }
.progress { height: 5px; margin-top: 7px; overflow: hidden; background: #3b505d; }
.progress span { display: block; width: 0; height: 100%; background: #69c5b5; transition: width .25s ease; }

.mode-nav { padding: 0 clamp(16px, 3vw, 42px); color: #fff; background: var(--nav-soft); border-bottom: 1px solid #415765; }
.mode-tabs { max-width: 1440px; margin: auto; display: flex; }
.mode-tab {
    min-height: 44px;
    padding: 10px 20px;
    color: #cbd7de;
    background: transparent;
    border: 0;
    border-bottom: 3px solid transparent;
    font-size: 13px;
    font-weight: 800;
}
.mode-tab:hover { color: #fff; background: rgba(255,255,255,.06); }
.mode-tab.active { color: #fff; border-bottom-color: #69c5b5; }

.year-nav { padding: 0 clamp(16px, 3vw, 42px); background: var(--paper); border-bottom: 1px solid var(--line); }
.year-tabs { max-width: 1440px; margin: auto; display: flex; overflow-x: auto; }
.year-tab {
    min-width: 76px;
    padding: 15px 18px 13px;
    color: var(--muted);
    background: transparent;
    border: 0;
    border-bottom: 3px solid transparent;
    font-weight: 700;
}
.year-tab:hover { color: var(--ink); background: var(--soft); }
.year-tab.active { color: var(--accent-dark); border-bottom-color: var(--accent); }

.app-shell {
    width: min(1440px, calc(100% - 32px));
    min-height: calc(100vh - 140px);
    margin: 24px auto 38px;
    display: grid;
    grid-template-columns: 270px minmax(0, 1fr);
    background: var(--paper);
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}
.app-shell.free-mode { grid-template-columns: minmax(0, 1fr); }
.app-shell.free-mode .task-sidebar { display: none; }

.task-sidebar { background: var(--soft); border-right: 1px solid var(--line); }
.sidebar-heading { padding: 21px 20px 16px; border-bottom: 1px solid var(--line); }
.sidebar-heading span { display: block; color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; }
.sidebar-heading strong { display: block; margin-top: 4px; font-size: 16px; }
.task-list { padding: 10px 0 18px; }
.model-title { margin: 13px 18px 6px; color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; }
.task-button {
    width: 100%;
    min-height: 46px;
    padding: 9px 16px 9px 20px;
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr) 16px;
    align-items: center;
    gap: 8px;
    color: #41515e;
    text-align: left;
    background: transparent;
    border: 0;
    border-left: 3px solid transparent;
}
.task-button:hover { background: #e9eef1; }
.task-button.active { color: var(--ink); background: var(--paper); border-left-color: var(--accent); }
.task-number { font-weight: 800; font-size: 13px; }
.task-summary { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.task-check { color: #a6b0b7; font-size: 14px; }
.task-button.solved .task-check { color: var(--green); }

.workspace { min-width: 0; padding: clamp(22px, 4vw, 48px); }
.task-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; }
.eyebrow { color: var(--accent-dark); font-size: 12px; font-weight: 800; text-transform: uppercase; }
h1 { margin: 6px 0 0; font-size: clamp(24px, 3vw, 34px); line-height: 1.15; letter-spacing: 0; }
.task-state { padding: 6px 10px; color: var(--muted); background: var(--soft); border: 1px solid var(--line); font-size: 12px; font-weight: 700; }
.task-state.solved { color: var(--green); background: var(--green-soft); border-color: #a9d7c2; }
.task-prompt { max-width: 940px; margin: 22px 0 26px; font-size: 17px; line-height: 1.6; }

.schema { margin: 0 0 26px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.schema summary { padding: 13px 2px; color: #344451; font-weight: 800; cursor: pointer; }
.schema-legend { padding: 2px 2px 9px; display: flex; align-items: center; gap: 16px; color: var(--muted); font-size: 10px; }
.schema-legend b { display: inline-grid; min-width: 22px; height: 17px; place-items: center; margin-right: 3px; color: var(--accent-dark); background: var(--accent-soft); border: 1px solid #add5ce; font-size: 8px; }
.schema-reset { margin-left: auto; padding: 4px 7px; color: var(--muted); background: transparent; border: 1px solid transparent; font-size: 10px; }
.schema-reset:hover { color: var(--ink); background: var(--soft); border-color: var(--line); }
.schema-scroll { padding: 4px 0 20px; overflow-x: auto; }
.schema-diagram { position: relative; min-width: 780px; padding: 12px 18px; background: #f8fafb; border: 1px solid var(--line); }
.schema-grid { position: relative; z-index: 2; display: grid; grid-template-columns: repeat(3, minmax(210px, 1fr)); gap: 42px 62px; align-items: start; }
.relation-layer { position: absolute; z-index: 1; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.relation-line { fill: none; stroke: #6d8798; stroke-width: 1.5; }
.relation-dot { fill: #fff; stroke: #557486; stroke-width: 1.5; }
.relation-label { fill: #435d6d; font: 700 9px/1 "Segoe UI", Arial, sans-serif; }
.schema-table { position: relative; min-width: 0; background: var(--paper); border: 1px solid #aebdc7; box-shadow: 0 3px 9px rgba(23, 33, 43, .06); will-change: transform; }
.schema-table.dragging { z-index: 4; box-shadow: 0 10px 24px rgba(23, 33, 43, .2); }
.schema-table > strong { display: block; padding: 9px 10px; overflow-wrap: anywhere; color: #fff; background: var(--nav-soft); cursor: grab; touch-action: none; user-select: none; font: 700 11px/1.35 ui-monospace, SFMono-Regular, Consolas, monospace; }
.schema-table.dragging > strong { cursor: grabbing; background: var(--accent-dark); }
.schema-fields { margin: 0; padding: 4px 0; list-style: none; }
.schema-field { min-height: 27px; padding: 5px 8px; display: grid; grid-template-columns: 28px minmax(0, 1fr); align-items: center; border-bottom: 1px solid #edf1f3; color: #435563; font: 11px/1.35 ui-monospace, SFMono-Regular, Consolas, monospace; }
.schema-field:last-child { border-bottom: 0; }
.schema-field.is-key { color: #172b38; font-weight: 700; }
.key-type { min-height: 16px; display: grid; place-items: center; color: var(--accent-dark); background: var(--accent-soft); border: 1px solid #add5ce; font: 700 8px/1 "Segoe UI", Arial, sans-serif; }
.key-type:empty { visibility: hidden; }
.field-name { padding-left: 7px; overflow-wrap: anywhere; }

.editor-section { border: 1px solid #b9c6cf; background: #fbfcfd; }
.editor-toolbar, .editor-actions { min-height: 48px; padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.editor-toolbar { border-bottom: 1px solid var(--line); }
.editor-toolbar label { font-size: 13px; font-weight: 800; }
.editor-toolbar > span, .select-only { color: var(--muted); font-size: 11px; }
kbd { padding: 2px 5px; background: #fff; border: 1px solid #cfd8de; box-shadow: 0 1px 0 #c3cdd4; font: 10px ui-monospace, SFMono-Regular, Consolas, monospace; }
#sqlEditor, #freeSqlEditor {
    width: 100%;
    min-height: 180px;
    padding: 18px;
    resize: vertical;
    display: block;
    color: #14202a;
    background: #fff;
    border: 0;
    border-bottom: 1px solid var(--line);
    outline: none;
    font: 14px/1.65 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
    tab-size: 4;
}
#sqlEditor:focus, #freeSqlEditor:focus { box-shadow: inset 0 0 0 2px rgba(8, 122, 107, .25); }
.run-button { min-height: 38px; padding: 8px 15px; color: #fff; background: var(--accent); border: 1px solid var(--accent-dark); font-weight: 800; }
.run-button:hover { background: var(--accent-dark); }
.run-button:disabled { cursor: wait; opacity: .65; }
.run-button span { margin-right: 5px; font-size: 10px; }

.feedback { margin-top: 20px; padding: 16px 18px; display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 12px; border: 1px solid; }
.feedback[hidden] { display: none; }
.feedback.correct { color: var(--green); background: var(--green-soft); border-color: #8fc9ad; }
.feedback.almost { color: var(--amber); background: var(--amber-soft); border-color: #e5c878; }
.feedback.wrong, .feedback.error { color: var(--red); background: var(--red-soft); border-color: #e4a5af; }
.feedback-icon { width: 24px; height: 24px; display: grid; place-items: center; border: 2px solid currentColor; border-radius: 50%; font-weight: 900; }
.feedback h2 { margin: 0; font-size: 15px; letter-spacing: 0; }
.feedback p { margin: 4px 0 0; color: var(--ink); line-height: 1.45; }

.result-section { margin-top: 28px; }
.result-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; }
.result-heading h2 { margin: 0 0 10px; font-size: 16px; letter-spacing: 0; }
.result-heading span { color: var(--muted); font-size: 12px; }
.table-scroll { overflow: auto; border: 1px solid var(--line); }
table { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
th { position: sticky; top: 0; color: #fff; background: var(--nav-soft); font-size: 11px; text-transform: none; }
tbody tr:nth-child(even) { background: var(--soft); }
tbody tr:last-child td { border-bottom: 0; }
.empty-result { padding: 18px; color: var(--muted); text-align: center; }

noscript { display: block; padding: 20px; color: var(--red); background: var(--red-soft); }
.free-workspace { max-width: 1120px; width: 100%; margin: 0 auto; }
.free-editor { margin-top: 34px; }
.database-badge { padding: 7px 11px; color: var(--blue); background: var(--blue-soft); border: 1px solid #afd0e5; font-size: 12px; font-weight: 800; }

@media (max-width: 820px) {
    .topbar { align-items: flex-start; }
    .progress-wrap { width: 150px; }
    .brand div span { display: none; }
    .app-shell { width: 100%; margin: 0; grid-template-columns: 1fr; border: 0; box-shadow: none; }
    .task-sidebar { border-right: 0; border-bottom: 1px solid var(--line); }
    .sidebar-heading { padding: 14px 16px 8px; }
    .task-list { padding: 4px 10px 12px; display: flex; overflow-x: auto; gap: 7px; }
    .model-title { align-self: center; min-width: max-content; margin: 0 3px 0 8px; }
    .task-button { width: auto; min-width: 78px; min-height: 38px; padding: 8px 10px; grid-template-columns: 1fr 14px; border: 1px solid var(--line); border-left-width: 1px; background: #fff; }
    .task-button.active { border-color: var(--accent); }
    .task-summary { display: none; }
    .workspace { padding: 24px 16px 36px; }
    .schema-diagram { min-width: 680px; }
    .schema-grid { gap: 32px 42px; }
}

@media (max-width: 520px) {
    .topbar { min-height: 64px; padding: 10px 14px; }
    .brand-mark { width: 40px; height: 38px; }
    .brand strong { font-size: 15px; }
    .progress-wrap { width: 105px; font-size: 10px; }
    .year-nav { padding: 0; }
    .mode-nav { padding: 0; }
    .mode-tab { flex: 1; padding: 10px 8px; }
    .year-tabs { justify-content: flex-start; }
    .year-tab { min-width: 64px; padding: 13px 12px 11px; }
    .task-heading { gap: 12px; }
    .task-state { padding: 5px 7px; }
    .task-prompt { font-size: 15px; }
    .schema-diagram { min-width: 640px; }
    .editor-toolbar > span { display: none; }
    .editor-actions { align-items: stretch; flex-direction: column; }
    .run-button { width: 100%; }
}
