:root {
    --primary: #0f3b73;
    --primary-dark: #09264b;
    --primary-hover: #15549f;
    --bg: #f3f6fb;
    --text: #111827;
    --muted: #64748b;
    --border: #e5e7eb;
    --white: #ffffff;
    --danger: #b42318;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
    color: var(--text);
    background: var(--bg);
}

a {
    color: inherit;
    text-decoration: none;
}

label {
    display: block;
    margin: 14px 0 7px;
    font-weight: 700;
    color: #22304a;
}

input,
textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 13px;
    font: inherit;
    background: var(--white);
    color: var(--text);
    outline: none;
}

input:focus,
textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(15, 59, 115, 0.14);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 0 16px;
    font-weight: 700;
    cursor: pointer;
    transition: 0.18s ease;
}

.btn-primary {
    background: var(--primary);
    color: var(--white);
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-light {
    background: var(--white);
    color: var(--primary);
    border-color: var(--border);
}

.btn-light:hover,
.btn-light.is-active {
    border-color: var(--primary);
    background: #eaf1fb;
}

.btn-full {
    width: 100%;
}

.login-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #061936 0%, #0f3b73 56%, #15569e 100%);
}

.login-shell {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.login-card {
    width: min(420px, 100%);
    background: var(--white);
    border-radius: 16px;
    padding: 34px;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.25);
}

.login-card h1,
.login-card p {
    margin: 0 0 10px;
}

.login-card p {
    color: var(--muted);
    margin-bottom: 24px;
}

.brand-mark {
    width: 54px;
    height: 54px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    margin-bottom: 18px;
    background: var(--primary);
    color: var(--white);
    font-weight: 800;
    letter-spacing: 0;
}

.alert-error {
    margin: 14px 0;
    border: 1px solid #f3b7b2;
    border-radius: 8px;
    padding: 12px;
    background: #fff1f0;
    color: var(--danger);
}

.app-header {
    min-height: 76px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 18px;
    padding: 18px 32px;
    background: var(--white);
    border-bottom: 1px solid var(--border);
}

.app-header h1 {
    margin: 0;
    color: var(--primary);
    font-size: 24px;
}

.app-header span {
    display: block;
    color: var(--muted);
    margin-top: 4px;
}

.header-actions,
.form-actions,
.editor-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.container {
    width: min(1160px, calc(100% - 32px));
    margin: 30px auto;
}

.container.narrow {
    width: min(820px, calc(100% - 32px));
}

.section-head {
    margin-bottom: 18px;
}

.section-head h2,
.section-head p {
    margin: 0 0 6px;
}

.section-head p {
    color: var(--muted);
}

.project-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 18px;
}

.project-card,
.form-card,
.empty-state {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 10px 28px rgba(15, 59, 115, 0.06);
}

.project-card {
    padding: 20px;
    transition: 0.18s ease;
}

.project-card:hover {
    transform: translateY(-2px);
    border-color: rgba(15, 59, 115, 0.35);
}

.project-card h3 {
    margin: 0 0 10px;
    color: var(--primary);
}

.project-card p {
    min-height: 44px;
    color: var(--muted);
}

.project-card span {
    display: block;
    margin-top: 16px;
    color: #506179;
    font-size: 14px;
}

.empty-state,
.form-card {
    padding: 26px;
}

.empty-state p {
    color: var(--muted);
}

.form-card h2 {
    margin-top: 0;
    color: var(--primary);
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0 16px;
}

.form-actions {
    justify-content: flex-end;
    margin-top: 22px;
}

.editor-page {
    height: 100vh;
    overflow: hidden;
}

.editor-topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    min-height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 10px 16px;
    background: var(--primary);
    color: var(--white);
}

.editor-title {
    min-width: 0;
}

.editor-title strong,
.editor-title span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.editor-title span {
    margin-top: 3px;
    color: #cddbf0;
    font-size: 14px;
}

.editor-shell {
    height: 100vh;
    padding-top: 64px;
    display: grid;
    grid-template-columns: 240px 1fr;
}

.tool-panel {
    background: var(--white);
    border-right: 1px solid var(--border);
    padding: 18px;
    overflow-y: auto;
}

.tool-panel h2 {
    margin: 0 0 14px;
    color: var(--primary);
    font-size: 18px;
}

.tool-btn {
    width: 100%;
    min-height: 40px;
    margin-bottom: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #f8fafd;
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
}

.tool-btn:hover,
.tool-btn.is-active {
    background: #eaf1fb;
    border-color: var(--primary);
    color: var(--primary);
}

.control-group {
    margin-top: 20px;
}

.check-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 14px;
    font-weight: 700;
}

.check-row input {
    width: auto;
}

.save-status {
    min-height: 22px;
    margin-top: 18px;
    color: var(--muted);
    font-size: 14px;
}

.workspace {
    position: relative;
    min-width: 0;
    min-height: 0;
    background: #dce5f1;
}

#map,
#drawCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}

#drawCanvas {
    z-index: 500;
    pointer-events: none;
    touch-action: none;
}

@media (max-width: 820px) {
    .app-header,
    .editor-topbar {
        align-items: flex-start;
        flex-direction: column;
    }

    .editor-topbar {
        min-height: 118px;
    }

    .editor-shell {
        padding-top: 118px;
        grid-template-columns: 160px 1fr;
    }

    .tool-panel {
        padding: 12px;
    }

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

@media (max-width: 560px) {
    .container {
        width: min(100% - 20px, 1160px);
        margin: 18px auto;
    }

    .login-card {
        padding: 24px;
    }

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

    .tool-panel {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        max-height: 210px;
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }

    .tool-panel h2,
    .control-group,
    .check-row,
    .save-status {
        grid-column: 1 / -1;
    }

    .tool-btn {
        margin-bottom: 0;
    }
}

/* Editor Etapa 2 */
.editor-body {
    height: 100vh;
    overflow: hidden;
    background: var(--bg);
}

.editor-body .editor-topbar {
    min-height: 64px;
    background: var(--primary);
    color: var(--white);
}

.editor-body .editor-actions {
    align-items: center;
}

.editor-body .btn-light.mode-active,
.editor-body .btn-light.mode-active:hover {
    background: var(--white);
    border-color: var(--white);
    color: var(--primary);
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.22);
}

.editor-layout {
    height: 100vh;
    padding-top: 64px;
    display: grid;
    grid-template-columns: 272px minmax(0, 1fr);
}

.editor-layout .tool-panel {
    background: var(--white);
    border-right: 1px solid var(--border);
    padding: 14px;
    overflow-y: auto;
}

.tool-section {
    padding: 0 0 16px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.tool-section h2 {
    margin: 0 0 10px;
    color: var(--primary);
    font-size: 14px;
    text-transform: uppercase;
    letter-spacing: 0;
}

.tool-section label {
    margin-top: 10px;
    font-size: 13px;
    color: var(--text);
}

.tool-section input[type="range"] {
    padding: 0;
}

.tool-section input[type="color"] {
    height: 42px;
    padding: 4px;
}

.editor-layout .tool-btn {
    min-height: 42px;
    margin-bottom: 8px;
    border-color: var(--border);
    background: #f8fafc;
    color: var(--text);
}

.editor-layout .tool-btn:hover,
.editor-layout .tool-btn.active {
    background: #eaf1fb;
    border-color: var(--primary);
    color: var(--primary);
}

.editor-layout .tool-btn.primary-tool {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--white);
}

.editor-layout .tool-btn.primary-tool:hover {
    background: var(--primary-hover);
}

.editor-layout .tool-btn:disabled {
    cursor: not-allowed;
    opacity: 0.48;
}

.editor-layout .check-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.editor-layout .check-row input {
    width: auto;
}

.editor-layout .save-status {
    min-height: 22px;
    margin: 4px 0 12px;
    color: var(--muted);
    font-size: 13px;
}

.canvas-area {
    position: relative;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    background: #dbe4ef;
}

.canvas-area #map,
.canvas-area #drawCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}

.canvas-area #map {
    z-index: 1;
}

.canvas-area #drawCanvas {
    z-index: 2;
    pointer-events: none;
    touch-action: none;
}

.bottom-toolbar {
    display: none;
}

.bottom-tool-btn {
    min-width: 72px;
    min-height: 48px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--white);
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
}

.bottom-tool-btn.active {
    background: #eaf1fb;
    border-color: var(--primary);
    color: var(--primary);
}

.bottom-tool-btn.primary {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--white);
}

.hidden {
    display: none !important;
}

.tool-hint {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.35;
}

.block-list {
    display: grid;
    gap: 8px;
}

.block-btn {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 10px;
    background: #f8fafc;
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
    text-align: left;
}

.block-btn:hover,
.block-btn.active {
    background: #eaf1fb;
    border-color: var(--primary);
    color: var(--primary);
}

.block-icon {
    position: relative;
    flex: 0 0 auto;
    width: 28px;
    height: 28px;
    display: inline-block;
    border: 2px solid var(--primary);
    color: var(--primary);
}

.block-icon-poste {
    border-radius: 50%;
}

.block-icon-poste::before,
.block-icon-poste::after {
    content: "";
    position: absolute;
    background: var(--primary);
}

.block-icon-poste::before {
    left: 6px;
    right: 6px;
    top: 12px;
    height: 2px;
}

.block-icon-poste::after {
    top: 6px;
    bottom: 6px;
    left: 12px;
    width: 2px;
}

.block-icon-luminaria {
    border-radius: 50% 50% 8px 8px;
    border-top-color: transparent;
}

.block-icon-luminaria::before {
    content: "";
    position: absolute;
    left: -11px;
    top: 12px;
    width: 14px;
    height: 2px;
    background: var(--primary);
}

.block-icon-braco {
    border: 0;
}

.block-icon-braco::before {
    content: "";
    position: absolute;
    left: 6px;
    top: 4px;
    width: 16px;
    height: 18px;
    border-left: 2px solid var(--primary);
    border-top: 2px solid var(--primary);
    border-radius: 8px 0 0 0;
}

.block-icon-comando,
.block-icon-medidor,
.block-icon-transformador {
    border-radius: 5px;
}

.block-icon-comando::before,
.block-icon-medidor::before,
.block-icon-transformador::before {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    font-size: 13px;
    font-weight: 800;
}

.block-icon-comando::before {
    content: "C";
}

.block-icon-medidor::before {
    content: "M";
}

.block-icon-transformador {
    border-radius: 50%;
}

.block-icon-transformador::before {
    content: "T";
}

.block-icon-transformador::after {
    content: "";
    position: absolute;
    inset: 5px;
    border: 2px solid var(--primary);
    border-radius: 50%;
}

.selected-object-panel .object-actions-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.selected-object-panel .object-actions-grid .tool-btn {
    margin-bottom: 0;
    font-size: 12px;
}

.legend-panel {
    display: grid;
    gap: 8px;
}

.legend-item {
    display: grid;
    grid-template-columns: 26px 1fr;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #f8fafc;
    color: var(--text);
    font-size: 13px;
    line-height: 1.3;
}

.legend-symbol {
    width: 24px;
    height: 24px;
}

.legend-empty {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    place-items: center;
    padding: 20px;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
}

.modal-card {
    position: relative;
    z-index: 1;
    width: min(440px, 100%);
    background: var(--white);
    border-radius: 12px;
    padding: 22px;
    box-shadow: 0 24px 80px rgba(15, 23, 42, 0.32);
}

.modal-card h2 {
    margin: 0 0 8px;
    color: var(--primary);
}

.modal-card p {
    margin: 0 0 14px;
    color: var(--muted);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 18px;
}

.mobile-block-panel {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: 76px;
    z-index: 1300;
    max-height: min(420px, calc(100vh - 190px));
    overflow-y: auto;
    padding: 12px;
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 18px 48px rgba(15, 23, 42, 0.2);
}

.mobile-block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    color: var(--primary);
}

.mobile-block-header button {
    min-height: 34px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #f8fafc;
    color: var(--primary);
    font-weight: 700;
}

.mobile-block-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (max-width: 900px) {
    .editor-body .editor-topbar {
        min-height: 104px;
        align-items: flex-start;
        flex-direction: column;
        gap: 8px;
        padding: 10px 12px;
    }

    .editor-body .editor-actions {
        width: 100%;
        gap: 8px;
    }

    .editor-body .editor-actions .btn {
        min-height: 38px;
        padding: 0 12px;
    }

    .editor-layout {
        height: 100vh;
        padding-top: 104px;
        padding-bottom: 72px;
        grid-template-columns: 176px minmax(0, 1fr);
    }

    .editor-layout .tool-panel {
        padding: 10px;
    }

    .tool-section {
        margin-bottom: 10px;
        padding-bottom: 10px;
    }

    .tool-section h2 {
        font-size: 12px;
    }

    .editor-layout .tool-btn {
        min-height: 38px;
        font-size: 13px;
    }

    .bottom-toolbar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1200;
        display: flex;
        gap: 8px;
        padding: 10px;
        overflow-x: auto;
        background: rgba(255, 255, 255, 0.96);
        border-top: 1px solid var(--border);
        box-shadow: 0 -8px 24px rgba(15, 23, 42, 0.12);
    }
}

@media (max-width: 620px) {
    .editor-body .editor-topbar {
        min-height: 132px;
    }

    .editor-layout {
        padding-top: 132px;
        grid-template-columns: 1fr;
    }

    .editor-layout .tool-panel {
        display: none;
    }

    .bottom-tool-btn {
        min-width: 78px;
    }
}
